The mp() is guaranteed to be a tuple() having the atom 're_pattern' as its first element, to allow for matching in guards. The arity of the tuple() or the content of the other fields may change in future releases.
The arity of the tuple() or the content of the other fields may change in future releases.
--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Oops, forgot to markdown-ify that last email…
deftuple Widget, foo: :x, bar: nil, baz: nil, buz: nil
# ...
tmatch Widget[bar: 23, buz: 42] = data
{:x, _, 23, _, 42} = data
What you wrote is how elixir implements records (with some extra magic for record dispatch).
iex(1)> defrecord Widget, foo: :x, bar: nil, baz: nil, buz: nil
{:module, Widget,
<<70, 79, 82, 49, 0, 0, 22, 144, 66, 69, 65, 77, 65, 116, 111, 109, 0, 0, 1, 14, 0, 0, 0, 33, 13, 69, 108, 105, 120, 105, 114, 46, 87, 105, 100, 103, 101, 116, 8, 95, 95, 105, 110, 102, 111, 95, 95, 4, 100, 111, ...>>,
nil}
iex(2)> Macro.expand(quote do Widget[bar: 23, buz: 42] end, __ENV__.context(:match)) |> Macro.to_string
"{Widget, _, 23, _, 42}"
Sadly, I think that’s just an over-earnest warning about a contemplated backwards-incompatible API change. I’ve stumbled on a few passages of erlang docs that seem really unsure about their future arities. (Confident Erlang, maybe?)
On the other hand, you can interrogate specific parts of tuples agnostic of their size with `elem`, which is available in guards:
```elixir
def process_xmerl_tuple(data) when is_tuple(data) and elem(data, 0) == :foo do
# take that xmerl!
end
```
I’d love a definitive answer to this as well.
The plus thing is, if you are in a guard, you don't need to check for the tuple_size. If you try to access an element that does not exist, it will move to the next one (i.e. errors in guards do not leak).