Proposal: Extend the ^ operator

104 views
Skip to first unread message

Michał Muskała

unread,
Dec 7, 2016, 11:40:27 AM12/7/16
to elixir-l...@googlegroups.com
Hello everybody,

Today the hat/pin operator (^) only allows the variable as argument, but I think it would be completely valid for it to allow any expression. This feels like a natural extension, especially useful when dealing with map fields. The number of times I tried to write something like that, only to realise it will fail is "too damn high" :P

assert_received {:foo, ^map.foo}

The obvious fix is to transform this into

foo = map.foo
assert_received {:foo, ^foo}

But I wonder - couldn't this be done automatically by the compiler, to extract such things from the patterns in order to place them before the case expression? I could be missing something, especially around some semantic difference, but I though about this for some time, and couldn't see anything harmful.

Michał.

José Valim

unread,
Dec 7, 2016, 12:03:32 PM12/7/16
to elixir-l...@googlegroups.com
The problem is doing so inside case, receive, etc:

case var do
  {:ok, ^map.foo} -> ...
  {:error, ^map.bar} -> ...
end

The only way to implement this is by reading the value before the case and that would be counter intuitive.



José Valim
Skype: jv.ptec
Founder and Director of R&D


Michał.

--
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-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/6B434A06-5482-4CB8-BB76-18403C89DA7F%40muskala.eu.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages