DEV Community

Alex Cosmas
Alex Cosmas

Posted on

Understanding Protocols in Elixir

Protocols in Elixir are a way to define a set of functions that a module must implement. They are similar to interfaces in other programming languages.

In Elixir, protocols are used to define a common set of functions that a module must implement, but they do not enforce the implementation details.

Example: Card Game

Let's say you have a group of friends who want to play a game together. The game requires each player to have a set of skills, such as the ability to roll a dice, draw a card, or move a piece on a board. However, each player has their own unique way of implementing these skills.

In Elixir, you can define a protocol for the game that specifies the functions that each player must implement. For example, the GamePlayer protocol might have functions like roll_dice, draw_card, and move_piece.

Now, let's say you have two friends, Alice and Bob, who want to play the game. Alice is a beginner and wants to implement the GamePlayer protocol in a simple way, while Bob is an expert and wants to implement it in a more complex way.

Alice might implement the roll_dice function by simply rolling a physical dice and returning the result. She might implement the draw_card function by drawing a card from a deck and returning its value. And she might implement the move_piece function by moving a piece on the board a certain number of spaces.

Bob, on the other hand, might implement the roll_dice function by using a statistical model to simulate the roll of a dice. He might implement the draw_card function by using a machine learning algorithm to predict the value of the card. And he might implement the move_piece function by using a physics engine to simulate the movement of the piece on the board.

Even though Alice and Bob have implemented the GamePlayer protocol in different ways, they can still play the game together because they both implement the same functions. This is similar to how protocols work in Elixir.

In Elixir, you can define a protocol using the defprotocol macro. For example:

defprotocol GamePlayer do
  def roll_dice
  def draw_card
  def move_piece
end
Enter fullscreen mode Exit fullscreen mode

Now, any module that wants to implement the GamePlayer protocol must implement these three functions.

For example, Alice's simple implementation of the GamePlayer protocol might look like this:

defmodule Alice do
  def roll_dice, do: 1 + 1
  def draw_card, do: 10
  def move_piece, do: 5
end
Enter fullscreen mode Exit fullscreen mode

Bob's more complex implementation of the GamePlayer protocol might look like this:

defmodule Bob do
  def roll_dice, do: simulate_dice_roll()
  def draw_card, do: predict_card_value()
  def move_piece, do: simulate_piece_movement(5)

  def simulate_dice_roll, do: ...
  def predict_card_value, do: ...
  def simulate_piece_movement(spaces), do: ...
end
Enter fullscreen mode Exit fullscreen mode

Even though Alice and Bob have implemented the GamePlayer protocol in different ways, they can still play the game together because they both implement the same functions.

This is the power of protocols in Elixir. They allow different modules to implement the same functions in different ways, making it easier to write reusable code and create more flexible programs.

Top comments (0)