-
Notifications
You must be signed in to change notification settings - Fork 56
Description
I'd like to see something about situations where you would prefer to use protocols over behaviors, or vice-versa. This is a hard decision to make, but I think I've developed a preference for protocols.
In general, I've seen behaviours misused more than protocols, where a behaviour will expect a struct of the using module's type, basically re-creating protocols but without some compiler niceness. Passing around a module name and then calling a function on it always feels awkward. I'd rather call a function on a named module so I can make sure the function exists at compile-time. I think it's harder to misuse protocols, especially because I do think they would still work in a lot of situations where behaviours would.
Here are a few things I read about this subject which can make clear that it's a hard choice:
- https://stackoverflow.com/questions/33704618/why-is-elixirs-access-behaviour-not-a-protocol
- https://web.archive.org/web/20180828125323/http://manuelalb.com/elixir/polymorphism/2017/02/14/behaviours_vs_protocols.html
- https://web.archive.org/web/20181127095939/https://www.djm.org.uk/posts/elixir-behaviours-vs-protocols-what-is-the-difference/
I would welcome a discussion; like I said this is a very nuanced decision where I think some advice would be very useful.