Appearance
Protocols
Read first: Structs
Source: Elixir guide, Protocols.
A ProtocolA mechanism for polymorphism: the behavior depends on the data type, and the dispatch is on the type of the first argument. Anyone can implement a protocol, at any time.Read the page · Glossary is polymorphism by data type. Here is the problem. A type/1 function with one clause for each type works while the code is yours, but nobody else can extend it. With a protocol, anyone can implement it, for any type, at any time.
elixir
defprotocol Utility do
@spec type(t) :: String.t()
def type(value)
end
defimpl Utility, for: BitString do
def type(_value), do: "string"
end
defimpl Utility, for: Integer do
def type(_value), do: "integer"
endThe defimpl blocks can be in different files. The dispatch is always on the type of the first argument.
Example: Size
elixir
defprotocol Size do
@doc "Calculates the size (and not the length!) of a data structure"
def size(data)
end
defimpl Size, for: BitString do
def size(string), do: byte_size(string)
end
defimpl Size, for: Map do
def size(map), do: map_size(map)
end
defimpl Size, for: Tuple do
def size(tuple), do: tuple_size(tuple)
endLists have no implementation, because the length of a list is not pre-computed. Size.size([1, 2, 3]) raises Protocol.UndefinedError. You can implement a protocol for Atom, BitString, Float, Function, Integer, List, Map, PID, Port, Reference, and Tuple.
Protocols and structs
A StructAn extension that is built on top of maps. It adds compile-time checks and default values.Read the page · Glossary is a map, but it does not share the implementations of the map. Size.size(%MapSet{}) fails until you write defimpl Size, for: MapSet. A struct needs its own implementation. This lets your own data types define what size means for them.
Fallback to Any
It is tedious to write every implementation by hand. Implement the protocol for Any. Then use one of two methods:
- derive:
@derive [Size]on a struct. This is explicit, and many libraries use it. - fall back:
@fallback_to_any truein the protocol. This opts in every type. It is a poor default, so it is off.
elixir
defimpl Size, for: Any do
def size(_), do: 0
endUnder the hood Our own text. It is not from the official docs.
What a ProtocolA mechanism for polymorphism: the behavior depends on the data type, and the dispatch is on the type of the first argument. Anyone can implement a protocol, at any time.Read the page · Glossary call becomes. A call such as Enumerable.reduce(list, acc, fun) first asks which module handles this kind of value. Then it calls that module. When mix compiles a project, it consolidates each protocol. The first step then becomes a few type checks and one jump that ends in a constant: the name of the module, for example Enumerable.List.
The Protocol docs describe a consolidated call as "equivalent to invoking two remote functions". One function identifies the implementation and one function calls it. Consolidation applies when "all protocol implementations are known up-front". For this reason, take care with implementations that you define later, for example only in tests.
Sources: we disassembled with :beam_disasm after Protocol.consolidate/2 on Elixir 1.20. The checks are is_map, get_map_elements (to read the name of a StructAn extension that is built on top of maps. It adds compile-time checks and default values.Read the page · Glossary), is_atom, and select_val. The shape of the generated code can change between Elixir versions.
Built-in protocols
Enumerable: whatEnumworks with.String.Chars:to_string/1, and what"#{value}"calls."#1"fails because TupleA fixed collection with curly brackets that can hold any value. Its elements are stored one after the other in memory, so it is fast to read one element by index or to get the size.Read the page · Glossary do not implement it.Inspect:inspect/1, and how IEx prints results. If the output starts with#, it is not valid Elixir, so inspect is not reversible.
Check yourself
Answer in your head. Then open the answer.
What decides which implementation of a protocol runs?
The data type of the first argument.