"1234567" + "89" // '123456789'
"1234567" - "345" // '1267'
"1234567" + "89" // '123456789'
This is DOA, because you cannot change what
But even if you could, why break
a-b instead of fixing broken
For my own curiosity, how do you perceive it as broken? Doing a small survey of other languages, concatenation seem to be the norm.
In other languages I know, either
+ has a given function and coerces arguments, or
+ can be overloaded and its function determined by argument types. In JS
+ cannot be overloaded, its function is determined by argument values, and arguments are coerced at the same time.
For example in AWK, Perl, Lua,
+ is always addition. They coerce arguments to numbers (each has their own rules how). AWK doesn't have concatenation operator symbol, it simply concatenates adjacent expressions, Perl has
. , Lua has
Perl even has separate arithmetic and string comparison operators:
string x number multiplication (repeat) operator, vs arithmetic
number * number
+ for addition,
++ for concatenation, iirc.
In C++, Python,
+ is used for both addition and concatenation; but built-in overloads prevent you from mismatching argument types. In C++ the operator function is determined from argument types (and possibly implicit conversions). In Python, strictly speaking, you can have different overloads directly on values, but I haven't seen any code do that; overloads are customarily defined on types.
example grievance with JS +
console.log((n + 1) + " fish")
(wasn't allowed to use template strings, otherwise I would, of course)
Each of those
+ means something else. And it was made worse by "prettier", which was mandatory on that project and removed the inner parentheses because they're not needed for precedence. But imo they helped clarity.
In a sensible language, you'd have some way to distinguish addition from concatenation, or be forced to explicitly state your intent with conversion:
($n + 1) . " fish"
(n + 1) " fish"
(n + 1) .. " fish"
str(n + 1) + " fish"
Admittedly the last one is almost as bad as JS. Luckily both Python and JS now have a much better way to accomplish this, with format/template strings.
Operator overload is super fun and I can see the utility in a concat operator. I'm not convinced that
+ is broken without these (rad) features. I think this is more-so a critique for dynamically typed languages, the behavior of
n + 1 is undeterminable without knowing the type of
No, it is not an issue in dynamically typed language where
+ means only “addition of numbers” (in which case
n + 1 means always “coerce
n to a number, then add
Sorry, can you clarify:
+means only “addition of numbers” (in which case
n + 1means always “coerce
nto a number, then add
Is this the behavior that is desired or detested?
This is a bad idea for JS, because
str - str is valid and already defined as subtraction with coercion
toNumber. However, this is a good idea for an esolang.
But how does it work exactly? Does it remove substrings or chars? If it removes chars, how is a "char" defined? (code-unit, code-point, or grapheme cluster?) What happens if the chars to be removed aren't present in the left-side string?