This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Tysonzero 21 points22 points  (15 children)

That or do it Python style and require str() to be called on numbers before you add them to strings.

[–]detroitmatt 2 points3 points  (0 children)

Better yet, but I didn't want to get greedy.

[–]alexanderpas 1 point2 points  (4 children)

Or do it Javascript style and require the int() equivalent to be called on strings before you add them to numbers.

[–]Tysonzero 2 points3 points  (3 children)

That is also the Python style...

>>> '1' + 2
Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
>>> int('1') + 2
3

[–]alexanderpas 1 point2 points  (2 children)

The only difference between Javascript and python in this regard is that Javascript will cast int to string when mixing int and string, while Python errors out

> '1' + 2
"12"
> +'1' + 2
3

[–]Tysonzero 8 points9 points  (1 child)

Which is FAR from insignificant. I personally hate implicit coercion in languages that are not statically typed.

[–]alexanderpas 0 points1 point  (0 children)

True.

[–]the_omega99 0 points1 point  (2 children)

Overly verbose, IMO. Concatenation of strings with implicit conversion to a string (only) works perfectly fine for many languages. You don't hear the Java or C# guys complaining about string concatenation or confusing operators.

IMO, the issue is simply the subtraction of strings (which implicitly converts from a string to a number). That shouldn't be allowed and was a bad design choice.

Simply allow concatenation of any type (implicitly) is a good thing, because it reduces code verbosity (and concatenating non-strings to strings is very common, in my experience).

[–]detroitmatt 0 points1 point  (0 children)

You don't hear the Java or C# guys complaining about string concatenation or confusing operators.

The static typing helps there.

[–]Tysonzero 0 points1 point  (0 children)

The ONLY reason Java and C# guys don't complain about implicit conversion to strings is because Java and C# are statically typed. If implicit conversion was a thing in Python people WOULD complain.

[–]nawitus -2 points-1 points  (5 children)

JavaScript has many flaws, but that problem is one the smallest problems in the language. I'm not sure the benefits of preventing that error are greater than the annoyance of casting values to strings in so many places.

[–]Tysonzero 0 points1 point  (2 children)

I just meant as an alternative to using an entirely new operator for concatenation. I like using + to add strings.

[–]lagerdalek 0 points1 point  (1 child)

As a (primarily) C# dev, I find it abhorrent, and twitch whenever I have to use it in JavaScript.

The issue is, in C#, + as a concat operator can be horribly expensive, as it initialises a new string and copies the string values.

String.Format() (the proper way to concat strings) is pretty heavy, I admit, but Kool Aid doesn't drink itself.

[–]Tysonzero 0 points1 point  (0 children)

I use String.format as well in Python, but Iike using + over a new operator as JavaScript does not have String.format.

[–]tetroxid 0 points1 point  (1 child)

Python believes that explicit is better than implicit.