5 ms·
"4" + 4 = "44" "44" - 4 = 40
by kamranjon 4y ago
"4" + 4 = "44"
"44" - 4 = 40
- deleted 4y ago[deleted]
- a_wild_dandan 4y agoIn other words: play stupid games, win stupid prizes. ;)
- kamranjon 4y agoHaha yes, most oddities in JS are a result of the language allowing you to play stupid games - many languages would just throw an error here (heck even ruby does). I discovered this strangeness when I forgot to parseFloat on a string and things kept humming along as if nothing was wrong, until I did some addition and things grew exponentially.
- cxr 4y ago> many languages would just throw an error here Some do. Many don't. I have a feeling that most people who say this sort of thing mean "Python" and assume that that's the norm. (The "heck even ruby does" remark def. reinforces that.) Can you name any that do that aren't dynamic languages?
- kamranjon 4y agoC, Rust, Go. There are probably many more. But I think you might be overall misinterpreting my original comment. I was responding to someone who said they still commonly find unpredictable behavior in Javascript that they were unaware of after using it for years. I just responded with something I recently discovered in a similar vein, and you appeared to take it as some sort of statement about Javascript as a language. I really enjoy Javascript, and I was really just highlighting a recent example I ran into that gave me the same feeling conveyed in the original comment I was responding to.
- cxr 4y agoC does not throw an error. C does not even have the infrastructure to throw errors. The closest thing are signals, and it doesn't use those for this issue. Go is similar to C, C#, Java, JS, etc in the sense that if anything is to be done about it then the expectation is for it to be flagged as part of typechecking before runtime. I don't have enough experience with Rust, but I'd be willing to bet that it doesn't do what you're saying, either.
- kamranjon 4y agoC throws a warning, because you need to manually cast to get expected behavior. But it does not behave the same as javascript, try it yourself, you will not get "44" when you add "4" to 4. Go does not support implicit type conversion, so no it does not behave the same as Java or Javascript, and will throw an error. Rust does not allow adding an integer to a string and will also throw an error. I suggest actually trying these things yourself, we are talking about adding an integer to a string, or subtracting an integer from a string. Obviously many languages will throw an error when trying to do this.
- cxr 4y ago> C throws a warning [sic] No, it doesn't. Nor does it throw an error. Once again: C does not even have the infrastructure to throw. Not just when adding numbers and strings, but anything. Ever. In any circumstance. > it does not behave the same as javascript Who said it did? > I suggest actually trying these things yourself Likewise (especially if you're going to be condescending about it). > Obviously many languages will throw an error when trying to do this. That's not obvious. I've asked you for examples, and you've named multiple ones that definitely don't. (And this is not an attempt to be rude, but rather frank: at this point in the discussion you come across as someone who, despite having probably used several handfuls of languages including the ones you mention, has a lot of confidence but only a coarse understanding and/or loose appreciation of the relevant concepts at play—down to confusing the distinction between compile-time vs run-time behavior.)
- eviks 4y agoUse stupid language, get stupid games
- mdp2021 4y agoTrue, and apparently counterintuitive if presented that way, but it makes sense as '+' is a string concatenation operator. It is the type conversion that can be used in unclear contexts - it can be comfortable and also dangerous.
- deleted 4y ago[deleted]
- cxr 4y agoPeople love to trot out examples like this while remaining totally silent about why they're trying to add/subtract numbers and strings in the first place or articulate what they expected to happen when they did it. On two occasions I have been asked, "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" [...] I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
- kamranjon 4y agoOh I absolutely ran into this because I had a bug, I had forgot to parse the number into the correct format, but it still feels odd that the language behaves this way. Here is an example of what Ruby outputs: "4" + 4 : `+': no implicit conversion of Integer into String (TypeError) "44" - 4 : undefined method `-' for "40":String (NoMethodError) The funny thing being the implicit type conversion that javascript does. This is likely why typescript has become so popular. Are there other languages that have implicit type conversion for basic types like javascript?
- plugin-baby 4y agoJava?
- mekoka 4y agoWhy? Because type errors are a fact of life in programming. They're not evidence of programmers trying to be eccentric. What was expected? Regardless if static or dynamic, it should be strongly typed. Just throw the error. All languages that have tried to salvage the situation when the programmer's intent became ambiguous end up creating problems down the road.
- cxr 4y ago> type errors are a fact of life in programming ...and anyone who cares, therefore, uses a typechecker. What makes it such a weird criticism is that, in order for it to make sense, you have to satisfy all three of 1) being someone who is annoyed that the operators have these well-defined semantics for mixed types, 2) actually mixing types and doing so by accident rather than intentionally, but 3) nonetheless someone who refuses to use a typechecker. That's a special case of someone insistent on putting the wrong figures into the machine altogether... > Regardless if static or dynamic, it should be strongly typed. Just throw the error. Java and C# are two of the most popular and widely used strongly typed languages. They don't throw errors for this.