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

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 #2
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 #1
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.

Last Post!

mmport80

mmport80

Just back from holidays, thanks for all the replies! Each one thoughtful and thought provoking.

And why this place is one of the best places on the internet!

I found a nice blog post which explains Behaviours to me in terms of Java’s interfaces and Haskell’s records:

It’s an extremely circuitous way to understand Behaviours on an Elixir forum, but Haskell’s precision helps me.

Where Next?

Popular in Discussions Top

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
AlexMcConnell
The reason that Rails is as popular as it is is because it’s very easy for relatively inexperienced developers to get a lot of work done....
588 20070 166
New
AstonJ
If a newbie asked you about Phoenix Contexts, how would you explain the basics to them? Feel free to be as concise or in-depth as you li...
New
wmnnd
The Go vs Elixir thread got me thinking: Would it be too hard to implement a simple mechanism for creating Go-style static app binaries f...
New
mmmrrr
Just saw that dhh announced https://hotwire.dev/ Is it just me or is this essentially live view? :smiley: Although I like the “iFrame-e...
New
crispinb
On reading dhh’s latest The One Person Framework it strikes me that Phoenix with LiveView is already pretty much this. However, never hav...
New
shishini
I think this twitter post and youtube video didn’t get as much attention as I hoped I am still new to Elixir, so can’t really judge ...
New

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54092 488
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54996 245
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44167 214
New

We're in Beta

About us Mission Statement