3 ms·
the problem is NOT static typing vs dynamic typing but weak typing vs strong typing and type coercion. In basic ruby ,unless you mess with classes, [1,2]+1 W
by camus2 13y ago
the problem is NOT static typing vs dynamic typing but weak typing vs strong typing and type coercion.
In basic ruby ,unless you mess with classes, [1,2]+1 WILL fail , though ruby is not staticaly typed. Ruby is STRONGLY typed, and that's a great thing.
Javascript will do everything it can not to fail:
String([1,2]) => "1,2"
+ 1 => "1,21"
IMHO
That's madness,because Javascript is basically making a decision for you.That was acceptable in 1995, that is not in 2013.
And that's its biggest flaw.Writing javascript means working against the language.
But there is a reason for that. Javascript is not good at throwing errors or handling them, at all.
So it's not about static vs dynamic typing.
There is no good solution for that problem unless TC39 decides to fix it. Enforcing dependency injection concepts and duck typing might work, but not always. For instance, you could check if a string is a string with
function(x){
x=x.substr(); // will throw a TypeError if I pass an Array.
}
Before doing anything with x, it might help reduce some mistakes. and you might use it on return types. But it's verbose and doesnt work all the time.
- nfoz 13y agoIt was not acceptable in 1995, either.