- Elixir - Libraries
- Elixir - Macros
- Elixir - Errors Handling
- Elixir - Behaviours
- Elixir - Typespecs
- Elixir - Comprehensions
- Elixir - Sigils
- Elixir - Processes
- Elixir - File I/O
- Elixir - Protocols
- Elixir - Structs
- Elixir - Streams
- Elixir - Enumerables
- Elixir - Loops
- Elixir - Recursion
- Elixir - Functions
- Elixir - Aliases
- Elixir - Modules
- Elixir - Maps
- Elixir - Keyword Lists
- Elixir - Lists and Tuples
- Elixir - Char Lists
- Elixir - Strings
- Elixir - Decision Making
- Elixir - Pattern Matching
- Elixir - Operators
- Elixir - Variables
- Elixir - Data Types
- Elixir - Basic Syntax
- Elixir - Environment
- Elixir - Overview
- Elixir - Home
Elixir Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Epxir - Protocols
Protocols are a mechanism to achieve polymorphism in Epxir. Dispatching on a protocol is available to any data type as long as it implements the protocol.
Let us consider an example of using protocols. We used a function called to_string in the previous chapters to convert from other types to the string type. This is actually a protocol. It acts according to the input that is given without producing an error. This might seem pke we are discussing pattern matching functions, but as we proceed further, it turns out different.
Consider the following example to further understand the protocol mechanism.
Let us create a protocol that will display if the given input is empty or not. We will call this protocol blank?.
Defining a Protocol
We can define a protocol in Epxir in the following way −
defprotocol Blank do def blank?(data) end
As you can see, we do not need to define a body for the function. If you are famipar with interfaces in other programming languages, you can think of a Protocol as essentially the same thing.
So this Protocol is saying that anything that implements it must have an empty? function, although it is up to the implementor as to how the function responds. With the protocol defined, let us understand how to add a couple of implementations.
Implementing a Protocol
Since we have defined a protocol, we now need to tell it how to handle the different inputs that it might get. Let us build on the example we had taken earper. We will implement the blank protocol for psts, maps and strings. This will show if the thing we passed is blank or not.
#Defining the protocol defprotocol Blank do def blank?(data) end #Implementing the protocol for psts defimpl Blank, for: List do def blank?([]), do: true def blank?(_), do: false end #Implementing the protocol for strings defimpl Blank, for: BitString do def blank?(""), do: true def blank?(_), do: false end #Implementing the protocol for maps defimpl Blank, for: Map do def blank?(map), do: map_size(map) == 0 end IO.puts(Blank.blank? []) IO.puts(Blank.blank? [:true, "Hello"]) IO.puts(Blank.blank? "") IO.puts(Blank.blank? "Hi")
You can implement your Protocol for as many or as few types as you want, whatever makes sense for the usage of your Protocol. This was a pretty basic use case of protocols. When the above program is run, it produces the following result −
true false true false
Note − If you use this for any types other than those you defined the protocol for, it will produce an error.
Advertisements