Right now, when you write a list comprehension with a pattern match, if the
match fails the result is "discarded", so effectively the pattern match
acts as a filter. This is convenient in some cases, but doesn't leave an
obvious option to use when you wanted to use the pattern match only for
destructuring, not filtering. E.g: in
for {x,y} <- pairs do...
We expect 'pairs' to to only contain pairs. It'd be unexpected for there to
be anything else in there, but if by mistake (e.g. during refactoring)
something else manages to sneak in there we may silently discard it which
is a source of easy to miss bugs. This happened to us in the 'real world'
during a refactor where we started to return a list of %{id:, name:}
structs instead of a list of pairs from a function. Of course cases like
that can often be caught with test coverage, but it'd be nice to have some
way to explicitly say that we expect the pattern match to succeed, and
raise if not, so cases that slip through the cracks don't lead to hard to
trace bugs.
In Erlang, with https://www.erlang.org/eeps/eep-0070 we have strict
comprehensions now with the syntax <:-.. I don't know if that syntax would
be appropriate for Elixir, but if not I am sure we could think of some
other syntax for it.
Apologies if this had already been discussed, I couldn't find a discussion
on it other than a comment by Jose on this github issue that it would have
to be something to be discussed
separately:
https://github.com/elixir-lang/elixir/issues/14148#issuecomment-3126519887
--
You received this message because you are subscribed to the Google Groups
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/elixir-lang-core/34bd1126-03aa-4859-a23b-5c69bf351d3dn%40googlegroups.com.