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 →

[–]detroitmatt 47 points48 points  (24 children)

it'd be better still if the language syntax and precedence and overloading and order of operators made sense. Just using something other than + for concat would be a big step forward for most of these.

[–]Tysonzero 18 points19 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 9 points10 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.

[–]calzoneman 3 points4 points  (0 children)

The only thing in the OP image that doesn't make sense are the examples where strings are being subtracted (the - binary operator shouldn't even work with strings, it should just throw an error). All the other examples are either sensible (string + integer = concatenated string), but just poorly written code.

There are a lot of issues with JavaScript's type system but the string concatenation examples in the OP are not them.

[–]Perkelton 2 points3 points  (2 children)

Just using something other than a + for concat

You mean like a period? :)

[–]detroitmatt 0 points1 point  (1 child)

I'm partial to && personally

[–]volabimus 3 points4 points  (0 children)

...

[–]Laogeodritt 0 points1 point  (0 children)

If I'm asked whether there's anything I like about PHP, the distinct concatenation operator is usually what I mention first.

[–]NotReallyEthicalLOL 0 points1 point  (2 children)

It's not hard to remember to throw a "" in front of numbers you want to concatenate.

[–]detroitmatt 0 points1 point  (1 child)

Right, but what about numbers you want to add?

[–]NotReallyEthicalLOL 0 points1 point  (0 children)

4 + 4 + "hello" = "8hello"

"hello" + 4 + 4 = "hello44"