Hacker News new | ask | show | jobs
by judofyr 2464 days ago
> What is the result of "badger"+2

The same result as `convert_to_number("badger") + convert_to_number(2)`. In Z it seems that converting an invalid string to number yields NaN, so "badger" + 2 returns NaN.

> There is a good reason to not define it because it is not associative: in JavaScript 2+2+"x" != 2+(2+"x")

The problem with + not being associative in JavaScript is because it's actually two different operators: String concatenation and number addition. Z doesn't have this problem because + is only number addition.

Personally I prefer strong typing (and thus `string + number` throwing an error and/or being a compile-time error), but it seems like the way Z does coercing is very sensible and doesn't have the same issues as JavaScript.