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?