GPrimola

GPrimola

Using defprotocol with defstruct for "data inheritance"

Hey guys,

I was trying Protocols and I saw a possibility I’ve never saw before, the use of defprotocol with defstruct.
I haven’t gone too much further on the implications, but the code below works fine.

defprotocol Foo do
  defstruct [:bar]
  def bar(foo)    
end

defimpl Foo, for: Foo do
  def bar(foo), do: foo.bar
end

defprotocol Zoo do
  defstruct [:foo, :some]
end

defimpl Foo, for: Zoo do
  def bar(zoo), do: zoo.foo.bar
end

With the code above we can notice that the Foo protocol will also behave like a module, i.e., the functions can have a body.

This “pattern” could be useful for data inheritance/composition where we have an is relationship, like for instance when we have a case where Person “is a” User and Employee “is a” Person, and both Employee and Person structs would “inherit” (sorry the OO term) User’s attributes.

So, by using Protocols we could do:

defprotocol User do
  defstruct [:username, :password]

  def get_username(user)
end

defprotocol Person do
  defstruct [:user, :name, :age, :birth_date]

  def to_str(person)
end

# Here I could've also modeled Employee using defprotocol too
defmodule Employee do
  defstruct [:person, :wage, :department]
end

defimpl User, for: Person do
  def get_username(person), do: person.user.username
end

defimpl User, for: Employee do
  def get_username(employee), do: employee.person.user.username
end

defimpl Person, for: Employee do
  def to_str(employee) do
    "#{employee.person.name} #{employee.person.age} @ #{employee.department}"
  end
end

To be used like this:

iex(21)> employee = %Employee{person: %Person{user: %User{username: "gio"}, name: "Giorgio", age: "30+"}, department: "IT"}

iex(22)> User.get_username employee
"gio"
iex(23)> Person.to_str employee
"Giorgio 30+ @ IT"

My question is if Protocols were designed to be used this way too?

First Post!

Sebb

Sebb

From the guide:

dispatching on a protocol is available to any data type that has implemented the protocol

That is not true for your protocol. Only data types that have a specific shape would work.

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40082 209
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42576 114
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement