I’m in this no-experience-to-apprenticeship program and everyone in my class thinks type coercion is the greatest thing ever.
Just be better lmao
Who would use that kind of type coercion? Who? I want to see his face.
I take this as less of a “I can’t use this intuitive feature reliably” thing and more of a “the truth table will bite you in the ass when you least expect it and/or make a mistake” thing.
Just use a formatter. It’ll show you that the second one is two statements:
{}
(the empty block)+[]
coerce an empty array to a number:new Number(new Array())
I totally get that: use the right tools and you’ll be okay. This applies to many technologies in this space.
With respect, I still take this advice like hearing “look out for rattlesnakes if you’re hiking there.” It might be safer to just hike where there are no rattlesnakes, instead.
This is why I try my damnedest not to write in weakly typed languages.
string
+ object
makes no logical sense, but the language will be like “'no biggie, you probably meant string + string so let’s convert the object to string”! And so all hell breaks loose when the language’s assumption is wrong.
Some automatic conversion is fine.
a=3+0.2
print(“Hello {name}. You are {age} years old”)
That kind of thing. But the principle of least surprise definitely applies. If you get to the point where you’re adding two booleans and a string, I feel like the language should at least say something. At least until the technology exists for it to physically reach out of your screen and slap you.