What are Protocols in Elixir?

What are Protocols in Elixir?
Average rating: 0
(0 votes)

Thanks! You’ve rated this material!

Only the laziest one will not explore Elixir these days. This language is perspective and hard to learn, so it is even more magnetic for the newcomers to start. Elixir has several mechanisms that allow us to write expressive and intuitive code. But do you know what are Protocols in Elixir? Today we will tell you the whole truth about it. 

Protocols are similar to pattern matching, but they allow us to write more meaningful and context-specific code based on the datatype we’re dealing with.

Let’s take the example of a content-delivery website. This website has multiple types of content: audio clips, videos, texts, and whatever else you can think of. Each of these content types obviously has different attributes and metadata, so it makes sense for them to be represented by independent structs. Each of these types has a few different fields, most of them unique to the type. We also have a common :file field which will point to the file keeping the actual data.

Using protocols, we can easily define what the act of transcribing something means to each of our data types.

If you love experimenting with code, read here more.

And as a bonus find out about parallel concepts between the two – Elixir Supervision Trees vs. React Component Trees. Unexpected comparison, huh?

Rate this article, if you like it

Thanks! You’ve rated this material!

Got a project? Let's discuss it!

Mailing & Legal Address

Syndicode Inc. 340 S Lemon Ave #3299, Walnut CA, 91789, USA

Visiting & Headquarters address
Kyiv Sofiivska 1/2a, 01001, Kyiv, Ukraine
Dnipro Hlinky 2, of. 1003, 49000, Dnipro, Ukraine
Email info@syndicode.com
Phone (+1) 9035021111