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

RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New

Other popular topics Top

New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54996 245
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49134 226
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44608 311
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement