Are you passing around data or modules?
For a behavior like gen server, you are launching a new process by providing the module. The module can be anything that implements the gen server behavior.
For a protocol like Enumerable, you are passing in data. You can pass in any data that implements the Enumerable protocol.
--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
A protocol enables polymorphism. With protocols can define an interface that different types can have different implementations of. A behaviour on the other hand defines an interface for modules.
Behaviours are just a way to document an interface for modules and have the compiler tell you if you don’t adhere to that interface, there is no logic in behaviours. Protocols have logic to dispatch to an implementation depending on the type of value you pass it.
I would say that there is no overlap between when I would use behaviours or protocols, even though they may seem similar in functionality. Use protocols when you want to have different implementations depending on the type of value you are working with (see Inspect
and Access
in stdlib). Use behaviours when you don’t really have an associated type (for example inspect has the type you want the string representation of, access has the type you want to access keys on).
An example of a behaviour could be the interface for database adapters or the interface you have to adhere to when implementing a GenServer.
--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.