mmport80

mmport80

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

Showing Posts 1 to 7

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
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
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.

jeremyjh

jeremyjh

I think it is also worth pointing out that Behaviours are inherited from Erlang, whereas Protocols are unique to Elixir. In my mind Behaviours are more comparable to interfaces in languages like Java and C#, and Protocols are more like Haskell type classes, except they cannot be polymorphic only in the return type like they can be in Haskell. I don’t think its possible to implement mempty for example.

AleksandarFilipov

AleksandarFilipov

This talk helped me a lot on the topic.

Maybe you’ll find it as useful as i did.

OvermindDL1

OvermindDL1

That is why something like Enum.into/1 takes an argument that defines the type of the return (and why not ‘merge’ it since it is basically free at that point anyway).

mmport80

mmport80 OP

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.

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 92995 915
New
AstonJ
The obligatory hello world thread! Who are you and where are you from? :stuck_out_tongue:
4616 55835 594
New
caslu
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
New
arcanemachine
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
matt-savvy
Is there a word for the ~> symbol used in Version strings? Do you also just call it a Squiggle Arrow™ ?!
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews