Protocols enable polymorphism in Elixir. Define protocols with defprotocol:
Implement a protocol with defimpl:
Instead of sharing protocol implementation with maps, structs require their own protocol implementation.
With the above implementations, we can do:
Protocols let you dispatch to any data type, so long as it implements the protocol. This includes some built-in types such as Atom, BitString, Tuples, and others.
Other Examples
Poison allows developers to implement Encoders for their own structs for serializing them to JSON, by implementing the Poison.Encoder protocol.