[Proposal] Add compiler error when using = instead of <- in with statement

48 views
Skip to first unread message

Bruno Rafael

unread,
Aug 12, 2020, 8:42:11 PM8/12/20
to elixir-lang-core
Hello all,

I mistyped the match operator with an equal signal (=) using the with statement and the compiler didn't warn. When calling a function with args that don't match the condition of a with statement where the match operator is a = the code breaks. I don't know if it's the desired behavior but when using the with statement and some condition doesn't match I would expect it to go to the else statement. Should the compiler warn in these cases?

elixir 1.10.3
erlang 22.3.4.1

Screenshot from 2020-08-12 21-40-20.png

Regards,
Bruno

Greg Vaughn

unread,
Aug 12, 2020, 8:49:33 PM8/12/20
to elixir-l...@googlegroups.com
Any expression is allowed in the comma-separated steps of a with expression. You can put in IO.inspect(myvar) (which I often use for debugging purposes). A pattern match (=) is an allowed expression. It is not a bug.

-Greg Vaughn

> On Aug 12, 2020, at 7:42 PM, Bruno Rafael <brunora...@gmail.com> wrote:
>
> Hello all,
>
> I mistyped the match operator with an equal signal (=) using the with statement and the compiler didn't warn. When calling a function with args that don't match the condition of a with statement where the match operator is a = the code breaks. I don't know if it's the desired behavior but when using the with statement and some condition doesn't match I would expect it to go to the else statement. Should the compiler warn in these cases?
>
> elixir 1.10.3
> erlang 22.3.4.1
>
> <Screenshot from 2020-08-12 21-40-20.png>
>
> Regards,
> Bruno
>
> --
> 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/ef2b8e67-4fb1-4a7b-a223-bd1fe7ad2336n%40googlegroups.com.
> <Screenshot from 2020-08-12 21-40-20.png>

Austin Ziegler

unread,
Aug 13, 2020, 9:56:26 AM8/13/20
to elixir-l...@googlegroups.com
Also, the `<-` is the “capturing” match that goes to `else`; `=` is explicitly permitted for pattern matching invariants (e.g., there is no meaningful recovery from that failed pattern match).

-a



--
Reply all
Reply to author
Forward
0 new messages