سؤال

I'd like to know if it's possible in Elixir to guard for a specific protocol.

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

Or is there something to assert that f is specifically a HashDict for example ?

Thanks !

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top