[Proposal] range pattern matching in case without when

70 views
Skip to first unread message

Igor Silva

unread,
Mar 13, 2021, 7:26:41 AM3/13/21
to elixir-lang-core
Current behavior

case ?a do
  x when x in ?a..?z -> # code
  x when x in ?A..?Z -> # code
  _ -> # code
end

Desired behavior

case ?a do
  ?a..?z -> # code
  ?A..?Z -> # code
  _ -> # code
end

#GoiásouVilaNova

Amos King

unread,
Mar 13, 2021, 7:44:06 AM3/13/21
to elixir-l...@googlegroups.com
Would this match on an element in a range and on an equivalent range? ?a or ?a..?z would both match with ?a..?z

The proposed code looks nice and I thought was simpler at first. Considering it further made me think of the situation above. I want the explicitness of the current behavior to reduce the ambiguity.

Amos King
CEO
Binary Noggin

On Mar 13, 2021, at 06:26, Igor Silva <computador...@gmail.com> wrote:

Current behavior
--
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 elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/61ea5a90-6e3b-49a0-ad58-377959dd6aabn%40googlegroups.com.

José Valim

unread,
Mar 13, 2021, 7:46:56 AM3/13/21
to elixir-l...@googlegroups.com
Thanks for the proposal,

However, ?a..?z in a pattern should match on a range from ?a..?z. Similarly, x..y can be used to match on any range and extract its values.

Those are all valid and correct today, and introducing this proposal would break said semantics.

eksperimental

unread,
Mar 13, 2021, 8:01:31 AM3/13/21
to elixir-l...@googlegroups.com
You can covert the case
case ?a do
x when x in ?a..?z -> :lower_case
x when x in ?A..?Z -> :upper_case
_ -> :unknown
end


into a cond:

cond do
?a in ?a..?z -> :lower_case
?a in ?A..?Z -> :upper_case
true -> :unknown
end

Igor Silva

unread,
Mar 14, 2021, 10:15:06 AM3/14/21
to elixir-lang-core
I understand, how about it?

case ?a do
  in ?a..?z -> # code
  in ?A..?Z -> # code
  _ -> # code
end

Goiás ou Vila Nova?

Ben Wilson

unread,
Mar 15, 2021, 2:51:59 PM3/15/21
to elixir-lang-core
I think eksperimental was dead on when he said what you really want is `cond`.

```
cond do
val in ?a..?z
val in ?A..?Z
```

Changing `case` to have an implicit `where` in certain special cases is going to make it behave inconsistently compared to other places that take patterns. Optimizing the characters for this specific case isn't worth the implicit magic ImHO.

Wiebe-Marten Wijnja

unread,
Mar 15, 2021, 3:09:32 PM3/15/21
to elixir-l...@googlegroups.com

As addendum to this:

`cond` is often treated as if it were slower than `case`, and I've seen people try to rewrite readable `cond`-clauses to harder-to-read `case`-clauses to reap extra performance benefits.
(I'll admit: In the past I've been guilty of doing this as well.)

Benchmarking will however show you that in many cases this does not matter.
The BEAM compiler is clever enough to optimize repeated conditionals also outside `case`-clauses.


~Marten/Qqwy

OpenPGP_signature
Reply all
Reply to author
Forward
0 new messages