1

Elixir で特定のプロトコルを保護できるかどうか知りたいです。

def some_fun(f) when implement?(f, Dict.Behaviour), do: ...

または、たとえば f が特に HashDict であると主張するものはありますか?

ありがとう !

4

1 に答える 1

5

You can do:

iex> Enumerable.impl_for!([])
Enumerable.List

But it doesn't work in a guard. However, this is very often a bad practice, you should just invoke the protocol instead.

If you are worried specifically about HashDict, you can do: is_record(dict, HashDict) and it should work on guards.

于 2014-03-12T14:41:42.753 に答える