4 ms·
> The day JS stops making ’1’ + 2 a string and throw an error Please, no. I'm bitten by this in Python every other day, with its otherwise weak typing. It feel
by jraph 8y ago
> The day JS stops making ’1’ + 2 a string and throw an error
Please, no. I'm bitten by this in Python every other day, with its otherwise weak typing. It feels like you can do many horrible things with types in Python, but no, you shall explicitly cast those numbers to strings. It is being more pedantic than Java by doing this. This never caught a real error in my code but it did make it needlessly fail at runtime because I did not put "str()". I know that the newer Python 3.x versions provide a nice bash-like way to embed expressions in strings and would let me avoid the problem entirely, but this would prevent my code to run in many places (and "Hello, I'm {}".format(age) is painful, hence the new feature by the way).
Thank you for trying to save my back from unwanted implicit type casts, but provide me with static typing instead if you want to do so, and fail at compile time.
If you want '1' + 2 to fail in JavaScript (You might want to, especially when keys of objects are coerced to strings in Javascript, which is actually crappy, and when you are constantly fetching numbers as strings from the web page), use TypeScript. It's amazing and provides a far better way of handling silly type errors.
edit: and by the way, C++ is a bit surprising in this respect. "Hello" + 2 is equal to... "llo".
- anoncake 8y ago> Please, no. I'm bitten by this in Python every other day, If you don't know whether your variables contain strings or numbers you literally don't know what your are doing. > with its otherwise weak typing. Python is strongly typed. > It is being more pedantic than Java by doing this. That just means that Java is broken in that respect. > Thank you for trying to save my back from unwanted implicit type casts Making '"1" + 2 = "1twee"' work does not just require a type cast. You actually have to choose and generate one of the string representations of II. > This never caught a real error Yes, it did. You tried to concatenate a string with a number. That cannot be done. > Thank you for trying to save my back from unwanted implicit type casts, but provide me with static typing instead if you want to do so, and fail at compile time. Python is too dynamic for that to work. However Mypy works well enough to be useful.
- shakna 8y ago> edit: and by the way, C++ is a bit surprising in this respect. "Hello" + 2 is equal to... "llo". Only surprising if you think "Hello" is an object of a string type. If however you think of it as a pointer, then moving the pointer is entirely unsurprising, especially as a pointer "is" an integer.
- redwall_hp 8y agoThat's because you're adding two to a pointer, which goes to the zeroth element in an array of integers. Adding two to the pointer is incrementing the memory address, so your pointer is now "l" instead of "H."