Recently a beginner on reddit asked a question about why
a += if (true) 1 else 2 didn’t work. We know it’s because that desugars to
a = a + if (true) 1 else 2, and
if isn’t a simple expression therefore it isn’t a valid RHS for
But it got me thinking, why don’t we desugar into
x += y into
x = x.+(y) instead? This means that
y no longer needs to be a simple expression.
I think this disproportionately affects new users since they tend to be both using
var and not knowing how to solve this issue.
So I propose that desugaring
+= would wrap the RHS in parens if there are no parens already there. I believe this should be backwards compatible with existing code.