Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想知道 Elixir 是否可以保护特定的协议。
def some_fun(f) when implement?(f, Dict.Behaviour), do: ...
或者有什么可以断言 f 是专门的 HashDict 吗?
谢谢 !
你可以做:
iex> Enumerable.impl_for!([]) Enumerable.List
但它在守卫中不起作用。但是,这通常是一种不好的做法,您应该只调用协议。
如果您特别担心 HashDict,您可以这样做:is_record(dict, HashDict)它应该适用于警卫。
is_record(dict, HashDict)