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?
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 Likes
fuelen
July 29, 2022, 4:05pm
3
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 Likes
I will actually use both of your solutions.
Guard + map attribute pattern matching
Thanks!
Don’t you still have to know which one you have when you work with them?
I have a general purpose loop that doesn’t need to know which one is which.
Then I also have specific functions that precisely pattern match against either %Variation{}
or %VariationGroup{}
I just realized that what I’m looking after is polymorphism for structs.
But I don’t think this is available in Elixir. Is it?
You can define a Protocol and have separate implementation for each struct
2 Likes
But what if I also want an implementation that apply on both structs?
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 Likes