Protocols are a mechanism to achieve polymorphism in Elixir. Dispatching on a protocol is available to any data type as long as it implements the protocol. Let’s see an example.
In Elixir, we have two idioms for checking how many items there are in a data structure: length
and size
. length
means the information must be computed. For example, length(list)
needs to traverse the whole list to calculate its length. On the other hand, tuple_size(tuple)
and byte_size(binary)
do not depend on the tuple and binary size as the size information is pre-computed in the data structure.
Even if we have type-specific functions for getting the size built into Elixir (such as tuple_size/1
), we could implement a generic Size
protocol that all data structures for which size is pre-computed would implement.
The protocol definition would look like this:
Comments
0 comments
Please sign in to leave a comment.