You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Line (1) is parsed as [1,2,3,4] -- ([1,2] -- [3]) = [1,2,3,4] -- [1,2]. This is documented correctly but it seems extremely counter-intuitive: it seems to me that everyone parses line (1) as "take [1,2,3,4], then remove 1 and 2, then remove 3. It's also inconsistent with the behavior of "-" (subtraction for numbers)
Expected behavior
"--" should be left-associative so that the two lines above are equivalent.
The text was updated successfully, but these errors were encountered:
This is mostly due to consistency with ++ (which has to be right-associative for performance reasons) and consistency with erlang which made the same decision (see this thread).
Changing it would be a breaking change so I'm not sure we can go ahead.
I wonder if there are other options to mitigate this and avoid the surprise, such as:
warn in such cases and ask to add parentheses to make the intended order explicit
have the formatter add parentheses in such cases so x -- y -- z becomes x -- (y -- z) (making it explicit what will happen)
Elixir and Erlang/OTP versions
Erlang/OTP 26 [erts-14.2.5] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [jit] [dtrace]
Elixir 1.17.0 (compiled with Erlang/OTP 26)
Operating system
MacOS Sonoma 14.5
Current behavior
The "--" operator that 'subtracts' lists from each other has the highly unintuitive property that it's right-associative. Specifically:
Line (1) is parsed as
[1,2,3,4] -- ([1,2] -- [3]) = [1,2,3,4] -- [1,2]
. This is documented correctly but it seems extremely counter-intuitive: it seems to me that everyone parses line (1) as "take[1,2,3,4]
, then remove1
and2
, then remove3
. It's also inconsistent with the behavior of "-" (subtraction for numbers)Expected behavior
"--" should be left-associative so that the two lines above are equivalent.
The text was updated successfully, but these errors were encountered: