New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error messages for invalid expression in match #5649

Closed
grych opened this Issue Jan 12, 2017 · 3 comments

Comments

Projects
None yet
2 participants
@grych
Copy link
Contributor

grych commented Jan 12, 2017

Environment

  • Elixir & Erlang versions (elixir -v):
    Erlang/OTP 19 [erts-8.2] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
    Elixir 1.4.0
  • Operating system:
    OS X El Capitan

Current behavior

iex(1)> f = &List.first/1
&List.first/1
iex(3)> case f do
...(3)>   &List.first/1 -> 11
...(3)> end
** (CompileError) iex:4: invalid expression in match
    (stdlib) lists.erl:1354: :lists.mapfoldl/3

however the other way works as expected:

iex(3)> case &List.first/1 do
...(3)> f -> 22
...(3)> end
22

Expected behavior

As a first-class citizen, function should work in case as it works in other pattern matching cases.

iex(3)> case f do
...(3)>   &List.first/1 -> 11
...(3)> end
11


@josevalim

This comment has been minimized.

Copy link
Member

josevalim commented Jan 12, 2017

It is not about being first-class, the issue is that &List.first/1 is not a valid pattern. There are many other data types that are first-class, such as a PID, that are also not valid patterns.

For example, imagine you want to match exactly on the pid 0.10.0. There is no way you can write such as a pattern. You would first need to build the pid, store it in a variable, and then match on it. That's exactly what you can do with functions:

x = &List.first/1
case &List.first/1 do
  ^x -> true
end

There are also cases like maps, where patterns and values are different. %{} means the empty map as a value but means all maps in a pattern.

I believe there is very little interest in making functions valid patterns in Elixir. So I would recommend going the route of ^x.

@josevalim josevalim closed this Jan 12, 2017

@grych

This comment has been minimized.

Copy link
Contributor

grych commented Jan 12, 2017

I see. Actually I found this when I tried to use such syntax, but I agree the case is very rare. Maybe I did not understand what invalid expression in match means.

Thanks for explanation!

@josevalim

This comment has been minimized.

Copy link
Member

josevalim commented Jan 12, 2017

Ok, I will reopen this because we can definitely use a better error message. Plus using the term "expression" in there is not quite correct.

@josevalim josevalim reopened this Jan 12, 2017

@josevalim josevalim changed the title Function is not allowed in `case` condition, raising `invalid expression in match` Improve error messages for invalid expression in match Jan 12, 2017

adkron added a commit to BinaryNoggin/elixir that referenced this issue Jan 16, 2017

Clarify invalid pattern in match error
It was pointed out that `invalid expression in match` was unclear
because the "expression" that is referred to should is not actually an
expression. Since `case` can only match against a pattern it is clearer
to say `invalid pattern in match`.

Addresses Issue elixir-lang#5649

Amos King @adkron <amos@binarynoggin.com>

josevalim added a commit that referenced this issue Jan 17, 2017

Clarify invalid pattern in match error
It was pointed out that `invalid expression in match` was unclear
because the "expression" that is referred to should is not actually an
expression. Since `case` can only match against a pattern it is clearer
to say `invalid pattern in match`.

Addresses Issue #5649

Amos King @adkron <amos@binarynoggin.com>

Signed-off-by: José Valim <jose.valim@plataformatec.com.br>

@josevalim josevalim closed this Jan 18, 2017

AndrewDryga added a commit to AndrewDryga/elixir that referenced this issue Jan 28, 2017

Clarify invalid pattern in match error
It was pointed out that `invalid expression in match` was unclear
because the "expression" that is referred to should is not actually an
expression. Since `case` can only match against a pattern it is clearer
to say `invalid pattern in match`.

Addresses Issue elixir-lang#5649

Amos King @adkron <amos@binarynoggin.com>

Signed-off-by: José Valim <jose.valim@plataformatec.com.br>

ckampfe added a commit to ckampfe/elixir that referenced this issue Jul 22, 2017

Clarify invalid pattern in match error
It was pointed out that `invalid expression in match` was unclear
because the "expression" that is referred to should is not actually an
expression. Since `case` can only match against a pattern it is clearer
to say `invalid pattern in match`.

Addresses Issue elixir-lang#5649

Amos King @adkron <amos@binarynoggin.com>

Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment