cblavier

cblavier

Struct pattern matching

Hey there :waving_hand:

I have the 2 following structs.

defmodule Variation do
  @enforce_keys [:id, :attributes]
  defstruct [:id, :description, :attributes, :slots, :block]
end

defmodule VariationGroup do
  @enforce_keys [:id, :variations]
  defstruct [:id, :description, :variations]
end

I would like to pattern match both structs at once, based on the fact they share similar attributes: :id and :description.

Is this achievable through behaviors?

Marked As Solved

fuelen

fuelen

Another options:

def test(%module{}) when module in [Variation, VariationGroup], do: ...
def test(term) when is_struct(term, Variation) or is_struct(term, VariationGroup), do: ...

Also Liked

JohnnyCurran

JohnnyCurran

Just treat them as maps:

def my_func(%{id: id, attributes: attributes}) do
  # work
end

And if you need to access the underlying module:

def my_func(%{id: id, attributes: attributes} = entity) do
  entity.__struct__
end
michallepicki

michallepicki

You can define a Protocol and have separate implementation for each struct

michallepicki

michallepicki

If you want to share the implementation of a function from a protocol, you can move it to a different module and delegate there from all separate protocol implementations. Or you can create a separate protocol for the shared parts and pass a list of struct modules (or primitive types if applicable) to defimpl.

Last Post!

michallepicki

michallepicki

If you want to share the implementation of a function from a protocol, you can move it to a different module and delegate there from all separate protocol implementations. Or you can create a separate protocol for the shared parts and pass a list of struct modules (or primitive types if applicable) to defimpl.

Where Next?

Popular in Questions Top

ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
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
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
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