Let’s say I have a list of Structs and I want to pattern-match on it.
e.g.
[%MyStruct{key1: val}] = List.first [%MyStruct{key1: val1}, %MyStruct{key2: val2},...]
If it fails to pattern-match then instead of raising an error we want to set the value of val
to nil
.
Otherwise the value of val
will be val1
.
The right hand side value come from a variable which can be a list of structs like shown above or something else.
What’s a good way to set the value of val
to nil if we can’t pattern-match it!
Currently I’m doing something like this:
val = if Enum.empty?(variable) do
nil
else
%MyStruct.PresentationMaterials{key1: val} = List.first(variable)
val
end
What’s a better way to achieve the same thing?