JavaScript
-
Especially that + and - act differently. If + does string concattenation, - should also do some string action or throw an error in this situation.
- should also do some string action
Like what kind of string action?
"Hello" + " world" is what everyone can understand. Switch with "-" and it becomes pointless.
-
This is my favorite language: GHC Haskell
GHC Haskell:
GHCi> length (2, "foo") 1
Wait, now I need to know why.
* some time later *
I went to check why the hell this happened. It looks like the pair ("
(,)
") is defined as an instance ofFoldable
, for some reason, which is the class used by functions likefoldl()
andfoldr()
. Meanwhile, triples and other tuples of higher order (such as triples, quadruples, ...) are not instances ofFoldable
.The weirdest part is that, if you try to use a pair as a
Foldable
, you only get the second value, for some reason... Here is an example.ghci> foldl (\acc x -> x:acc) [] (1,2) [2]
This makes it so that the returned length is 1.
-
BS. A language shouldn't have operators that allow non sensical operations like string concatenation when one operand is not a string.
It's not nonsensical, implicit type coercion is a feature of JavaScript, it's perfectly logical and predictable.
JavaScript is a filthy beast, it's not the right tool for every job, but it's not nonsensical.
When you follow a string with a
+
, it concatenates it with the next value (converted to string if needed). This makes sense, and it's a very standard convention in most languages.Applying arithmetic to a string would be nonsensical, which they don't do.
-
Why would you need an entirely different way of concatenating strings? "11" + 1 -> exception. "11" + to_string(1) = "111"
You're right. I've got too much Perl on the brain and forgot my roots. There is a language that does what you're talking about with the '+' operator: BASIC
Good luck getting the same thing retrofitted into JavaScript though. I can imagine a large number of websites would break or develop mysterious problems if this (mis)behaviour was fixed.
-
This post did not contain any content.
To start off... Using arithmetic operators on strings in combination with integers is a pure skill issue. Let's disregard this.
If you were to use + where one part is a string, it's natural to assume a string appending is desired since + is commonly used as a function for this. On the other hand, - is never used for any string operation. Therefore, it's safe to assume that it relates to actual artihmetics and any strings should therefore be converted to numerical values.
This is an issue with untyped languages. If you don't like it, use typescript. End of story.
-
This has got to be baNaNa
wrote on last edited by [email protected]That is absolutely
(n > 1) * ("ba" + 0/0 + "a")
-
expressly designed to carry on regardless
I'm surprised they didn't borrow
On Error Resume Next
from Visual Basic. Which was wrongly considered to be the worst thing in Visual Basic - when the real worst thing wasOn Error Resume
.On Error Resume Next
at least moved on to the next line of code when an error occurred;On Error Resume
just executed the error-generating line again ... and again ... and again ... and again ... -
To start off... Using arithmetic operators on strings in combination with integers is a pure skill issue. Let's disregard this.
If you were to use + where one part is a string, it's natural to assume a string appending is desired since + is commonly used as a function for this. On the other hand, - is never used for any string operation. Therefore, it's safe to assume that it relates to actual artihmetics and any strings should therefore be converted to numerical values.
This is an issue with untyped languages. If you don't like it, use typescript. End of story.
Instead of trying to make it work, javascript could just say "error." Being untyped doesn't mean you can't have error messages.
-
Instead of trying to make it work, javascript could just say "error." Being untyped doesn't mean you can't have error messages.
This is fair enough from an idealistic view. In practice, you don't want your entire website to shit itself because of a potentially insignificant error.
-
Wait, now I need to know why.
* some time later *
I went to check why the hell this happened. It looks like the pair ("
(,)
") is defined as an instance ofFoldable
, for some reason, which is the class used by functions likefoldl()
andfoldr()
. Meanwhile, triples and other tuples of higher order (such as triples, quadruples, ...) are not instances ofFoldable
.The weirdest part is that, if you try to use a pair as a
Foldable
, you only get the second value, for some reason... Here is an example.ghci> foldl (\acc x -> x:acc) [] (1,2) [2]
This makes it so that the returned length is 1.
Oddly enough, in Haskell (as defined by the report), length is monomorphic, so it just doesn't work on tuples (type error).
Due to the way kinds (types of types) work in Haskell, Foldable instances can only operate over (i.e. length only counts) elements of the last/final type argument. So, for (,) it only counts the second part, which is always there exactly once. If you provided a Foldable for (,,,) it would also have length of 1.
-
This post did not contain any content.
Heck, I need to learn some new languages apparently. Here I was expecting an angry "CS0029 cannot implicitly convert type 'string' to 'int'!
-
- should also do some string action
Like what kind of string action?
"Hello" + " world" is what everyone can understand. Switch with "-" and it becomes pointless.
this the “or throw an error”
-
Hm, playing devil's advocate, I think it is because the minus has not been defined as a string operation (e.g. it could pop the last char), so it defaults to the mathematical operation and converts both inputs into ints.
The first is assumed to be a concat because one of the parcels is a string...
It's just doing a lot of stuff for you that it shouldn't be in first place 🤭
Yeah, this looks dumb on the surface, but you've got bigger problems if you're trying to do math with strings
-
Wait, now I need to know why.
* some time later *
I went to check why the hell this happened. It looks like the pair ("
(,)
") is defined as an instance ofFoldable
, for some reason, which is the class used by functions likefoldl()
andfoldr()
. Meanwhile, triples and other tuples of higher order (such as triples, quadruples, ...) are not instances ofFoldable
.The weirdest part is that, if you try to use a pair as a
Foldable
, you only get the second value, for some reason... Here is an example.ghci> foldl (\acc x -> x:acc) [] (1,2) [2]
This makes it so that the returned length is 1.
wrote on last edited by [email protected]I don't even know Haskell but it seems like (" ( , ) ") would be an instance of boob.
-
This is fair enough from an idealistic view. In practice, you don't want your entire website to shit itself because of a potentially insignificant error.
In practice runtime errors are a bitch to find and fix.
-
this the “or throw an error”
If you try what I wrote it will throw a NaN. I was asking about the first part of the proposal.
-
I don't even know Haskell but it seems like (" ( , ) ") would be an instance of boob.
wrote on last edited by [email protected]It looks like two worms split running from another tinier worm. Makes you wonder what it has done to be so feared
-
This is fair enough from an idealistic view. In practice, you don't want your entire website to shit itself because of a potentially insignificant error.
This is exactly why it should throw an error, to make it incredibly obvious something isn't working correctly so it can be fixed. Otherwise you have wrong logic leading to hard to notice and hard to debug problems in your code
-
This post did not contain any content.
That is just the tip of the iceberg:
-
It is 'comprehensible' in the sense that it's possible to figure out how it happened, but it absolutely does not "make sense" in terms of being a reasonable language design decision. It's 100% incompetence on the part of the person who created Javascript.
It makes perfect sense if the Lang objective is to fail as little as possible. It picks the left side object, checks if the operand is a valid operand of the type. If it is, it casts the right variable into that type and perform the operand. If it isn't, it reverses operand positions and tries again.
The issue here is more the fact that + is used both as addition and as concatenation with different data types. Well, not an issue, just some people will complain.