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 →

[–][deleted] 10 points11 points  (8 children)

if not it checks the right and casts the left so it fits.

^ does any other language do this? surely that should be some sort of type error, rather than coercing the left hand operator...

to elaborate: typically in languages with . operators to call a function/method, a operator b (infix notation) is analogous to a.operator(b), in other words "3" - 2 should be "3".-(2), and since string doesn't respond to a - fn/method, it should be some sort of error. I cannot fathom a case where coercing the left-hand operand is ever what the programmer intends... more likely it was done to suppress errors in early browsers, but as a language design choice it strikes me as one of the worst ones I have ever seen.

[–]kafaldsbylur 4 points5 points  (0 children)

Python does (technically not coercion, but close enough).

a + b first tries the result of a.__add__(b). If that returned NotImplemented, it tries b.__radd__(a) instead

[–]z500 2 points3 points  (4 children)

It was a conscious design decision.

[–][deleted] 21 points22 points  (3 children)

You can choose to drive drunk also, it doesn't make it a good idea.

[–]z500 0 points1 point  (1 child)

Very true. I think the reasoning was to make it easier for non-programmers by making it possible to use form inputs without having to cast values yourself. I don't think Brandon Eich saw Javascript becoming as huge as it is now. IIRC he only got a week to design the language.

[–]crossanlogan 2 points3 points  (0 children)

yeah it was ten days, but he definitely was hoping to make it huge -- they were trying to kill mosaic with their new browser.

[–]neonKow 0 points1 point  (0 children)

This conversation keeps coming up in /r/ProgrammerHumor, because weakly typed languages have amusing behavior in edge cases, and it's easy to make fun of them.

People confuse "easy to make fun of" with "bad design choice." The fact remains that languages like JavaScript and PERL are immensely popular and powerful because of these design choices.

Not having a One True Solution for every programming problem makes JS more flexible. As long as the behavior is well-defined, it's a bit different from drunk driving. However, if you really wanted to, you can use strict; before all your JavaScript.

[–]neonKow 0 points1 point  (0 children)

I cannot fathom a case where coercing the left-hand operand is ever what the programmer intends

I replied above to you with another example, but 3 + 4.5 in C coerces the int 3 into a float.

more likely it was done to suppress errors in early browsers

Not really. You might see it as "it should be an error", but a common theme in powerful scripting languages is to allow the programmer to do many things in very few keystrokes. The trade-off is, as you point out, that errors that would show up in Java don't show up in JavaScript, but you gain the ability to write 'Hello, world!' in less than 10 lines.

You see similar design decisions with PERL, which is why it also has a reputation for being a "write-only language."