4 ms·
At the risk of getting off in the weeds, I'd rather re-frame the problem here as to wether unary negation makes sense as an operation, at all! In my ideal worl
by aaronblohowiak 15y ago
At the risk of getting off in the weeds, I'd rather re-frame the problem here as to wether unary negation makes sense as an operation, at all!
In my ideal world, -EXPR would be a parse error, and it would only be valid to write -LITERAL * expr. (-1 * a.) So, you could have -4^2 is 16 because the - would not associate to the nearest constant, but rather be part of the 4-value.
The reason for this is that -a^2 is super confusing, and I believe it should be treated as invalid. I do not think that promoting the order of precedence of unary - is the best solution because it leads to the common confusion of thinking "well, a is already negative, so there is nothing to do". if instead this was explicitly -1 * a ^ 2, then you can always "plug and chug" with simpler rules.
Essentially, the confusion is because unary negation is the only operation that is confuse-able with the value definition.
Final qualm against unary negation: what does --4 evaluate to in ruby? 3? No. 4.
If I had my way, then --4 would clearly be invalid because the inner -4 could not receive the unary negation.
In more programming stuff, I think that mixed-type operations should be illegal (no implicit casts!) adding an int64 to an int32 and storing the result in an int32 should be a compiler error!