cblavier
Struct pattern matching
Hey there ![]()
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
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: ...
8
Also Liked
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
4
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.
2
Popular in Questions
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
New
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Hi,
I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project.
Baby step #1 is extracting the number ...
New
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
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Sometimes I want to check if the input into a function is not a blank string.
My first approach:
defmodule Example do
def do_stuff(s...
New
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
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
Other popular topics
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this:
...
New
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine)
This is a plugin that adds support for Elixir to JetBrains IntelliJ...
New
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
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







