mmport80

mmport80

Behaviours & Protocols

I have an intuitive understanding of behaviours and protocols of course, but after talking with a colleague today, I was surprised that behaviours and protocols are kind of the same thing.

Mainly because protocols are like type classes in Haskell, and if I squint, behaviours are also.

Which you know, is comforting, as I always like seeing links between data (protocols) and code / actions (behaviours).

But, where am I going wrong?

Can anyone explain these Elixir concepts in terms of Haskell, maybe a bit more precision will show me where I am going wrong..

Could I create a GenServer protocol? Or is that completely ridiculous?

I see a related link which is interesting: Why is Access a behaviour instead of a Protocol? When to use a Protocol instead of a Behaviour? - #2 by michalmuskala

Most Liked Responses

peerreynders

peerreynders

I see the intent behind either as being quite different. However historically OO has often conflated the notion of polymorphism and implementation inheritance - so it would be very easy to fall into the same trap.

  • Protocols are ultimately about polymorphism. One part is the interface that will give access to the same polymorphic behaviour. Then each data type wishing to expose that behaviour implements it separately.
  • Behaviours are about separating the generic implementation from the specific. A Behaviour captures the common implementation for reuse and uses callbacks to invoke the specific implementation (i.e. the part that varies - a Behaviour acts more like a framework).

Erlang OTP Design Principles: 1.2 Behaviours:

In OO the callback module would likely be implemented as a Strategy.

From a development perspective you are implementing an interface and you are implementing a callback module but that is focussing on the “what you are doing” and the “how you are doing it”.

The “why you are doing it” is quite different.

19
Post #3
NobbZ

NobbZ

A “behaviour” defines an API that a module has to provide.

A “protocol” is implemented on top of behaviours and chooses from a set of modules to use when dispatching function calls with data, based on their “type”.

11
Post #2
rvirding

rvirding

Creator of Erlang

To complement a bit on what @peerreynders wrote. In OTP behaviours are generally coupled to processes in that it is around a process where the generic and specific are defined. The generic typically describes the basic process interface and the specific which callbacks are required and when and how they are called/used. So for example a GenServer is a process with a defined interface and a set of 6 callbacks.

Where Next?

Popular in Discussions Top

cvkmohan
The upcoming Phoenix 1.6 release looks very interesting. Became a habit to watch the commits - and - what they are bringing in. phx.gen...
New
jeramyRR
This is an interesting article to read. Elixir’s performance, like usual, is excellent. However, it seems like the high CPU usage is co...
New
Fl4m3Ph03n1x
Background This question comes mainly from my ignorance. Today is Black Friday, one of my favorite days of the year to buy books. One boo...
New
mmport80
I have put far too much effort into Dialyzer over the last year or so - and basically - I doubt it’s worth the effort. It’s not as easy ...
New
nburkley
AWS re:Invent is on at the moment with some interesting announcements. One new feature in particular is the Lambda Runtime API for AWS La...
New
sergio
There’s a new TIOBE index report that came out that shows Elixir is still not in the top 50 used languages. It also goes on to call Elix...
New
rower687
Hi all, I’ve been reading a lot about the “let it crash” term and how supervising processes and the whole messaging passing make an elixi...
New
AstonJ
Can you believe the first professionally published Elixir book was published just 8 years ago? Since then I think we’ve seen more books f...
New
scouten
I’m looking for a host for the server part of a small (personal) side project that I’m working on. It’s currently written in Node.js and ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New

We're in Beta

About us Mission Statement