Failed to get return value map on method in elixir

Zhao Yi Source

I am using an elixir library Rox to manage data in rocksdb. There is a delete method defined as below:

delete(db_or_cf, key, write_opts \\ [])
delete(Rox.DB.t | Rox.ColumnFamily.t, key, write_options) ::
  :ok |
  {:error, any}

I am using below way to call this method:

result = case Rox.delete(db, :erlang.term_to_binary(key)) do
            {:ok} -> {:ok}
            {:error} -> {:error}
        end

But I got an error: (CaseClauseError) no case clause matching: :ok. I am new to elixir and I don't know why the :ok doesn't match.

elixir

Answers

answered 3 months ago Dogbert #1

The function returns either :ok or {:error, any()} so the patterns you need to match are :ok and {:error, error}. You have an extra {} around :ok and :error tuple should have a second element.

case Rox.delete(db, :erlang.term_to_binary(key)) do
  :ok -> ...
  {:error, error} -> ...
end

answered 3 months ago PatNowak #2

{:ok} is not :ok. First expression is one element tuple, second is simply an atom.

Try this:

result = case Rox.delete(db, :erlang.term_to_binary(key)) do
  :ok -> :ok
  {:error, _any} -> {:error, _any}
end

comments powered by Disqus