Reason: `++` is now left-associative

Created on 12 Oct 2017  路  9Comments  路  Source: reasonml/reason

Previously, Reason and OCaml's ^ are right-associative. Now it's left-associative. A small consequence of this is that the old snippet:

a ^ b ^ c

now converts to

a ++ (b ++ c)

instead of

a ++ b ++ c

cc @let-def

Most helpful comment

If ++ is left-associative, it has no impact on string concatenation, but if it was overloaded for list concatenation, then a chain of ++ degenerates to a quadratic runtime, whereas a right-associative one is linear.
++ should be right-associativefor this reason :P .

All 9 comments

@chenglou If in 1.13.7, it was left-associative, then when converting shouldn't it convert to a ++ b ++ c?

1.13.7 would parse a ^ b ^ c as (a ^ b) ^ c, and then when passing to 3.0.0's printer, it would print as a ++ b ++ c right?

I got the left/right wrong. Fixed. The issue's still here though

FWIW, it appears ^ was actually right associative in 1.13.7.

So I don't think there's anything wrong with this.
Except, you could make it so that ++ is also right associative in 3.0.0, so that when printing, there's no parens.
It might not matter for string concatenation because string concat is commutative.
It's worth seeing how it will play with |> and make the call based on that.

If this is easy to fix (make it right-associative) and doesn't cause other precedence issues, then let's do it. Otherwise it's no big deal and we can close this. Waiting for confirmation.

Only downside is that + is left associative so it would be confusing if ++ was right associative. Maybe it doesn't matter since ++ will nearly always be used for string concat which we know abides by the associative property a ++ (b ++ c) is always equal to (a ++ b) ++ c. It would be rare for someone to be bitten by an assumption that ++ was left associative when it's really right associative. Debugging might be a bit unintuitive.

I'm inclined to vote right associative for ++.

If ++ is left-associative, it has no impact on string concatenation, but if it was overloaded for list concatenation, then a chain of ++ degenerates to a quadratic runtime, whereas a right-associative one is linear.
++ should be right-associativefor this reason :P .

How often and typical is ++ overloaded to be list concatenation?

Done in #1598

Was this page helpful?
0 / 5 - 0 ratings

Related issues

braibant picture braibant  路  4Comments

jberdine picture jberdine  路  3Comments

rickyvetter picture rickyvetter  路  3Comments

TheSpyder picture TheSpyder  路  3Comments

TrakBit picture TrakBit  路  3Comments