Difference between protocol & behaviour in elixir

后端 未结 2 984
小鲜肉
小鲜肉 2020-12-29 19:55

Behaviours define callbacks & protocols define methods without signatures. Modules implementing a protocol should give definition for all those methods. Same for modules

2条回答
  •  时光说笑
    2020-12-29 20:51

    Protocol is type/data based polymorphism. When I call Enum.each(foo, ...), the concrete enumeration is determined from the type of foo.

    Behaviour is a typeless plug-in mechanism. When I call GenServer.start(MyModule), I explicitly pass MyModule as a plug-in, and the generic code from GenServer will call into this module when needed.

提交回复
热议问题