Hacker News new | ask | show | jobs
by sdegutis 2839 days ago
> "We fully embrace Javascript as dynamically typed language and when one does this, types on most cases will become irrelevant."

That's not dynamic typing, that's weak typing.

Dynamic typing just means the types are omitted in the source code, but they're still there. typeof 3 is still 'number'. If you explicitly specify the types, you get TypeScript.

But weak typing means you can do things with disparate types that you can't normally do, usually by implicitly coercing one to the same type as the other.

The world has generally considered weak typing a very bad idea. Type coercion has too many edge cases and unintuitive/unexpected combinations. Even Lua is experimenting with runtime flags that disable type coercion such as "1" + 2. (That's the reason they have the .. operator btw.)

The only byproduct of weak typing that most people agree on keeping in modern languages is the concept of "truthy", but even then, languages have different ideas of what should be truthy. Clojure and Ruby say everything except nil and false are truthy, Python says "" and [] are also falsey, and I don't remember where JavaScript fully stands on this, but I know that null, undefined, false, and "" are at least falsey.

2 comments

Yep, you are correct about generic truthy. We have standard library function empty(), which is custom (agreed internally what is empty) and which everyone knows. That solves many issues you pointed.
> The world has generally considered weak typing a very bad idea.

Now that is the type of thing that screams for supporting evidence.

> Type coercion has too many edge cases and unintuitive/unexpected combinations.

That depends on the implementation. I would say Perl handles this problem neatly by making the coercion done entirely based on the operator used, so it's always obvious.

> Now that is the type of thing that screams for supporting evidence.

I think the direction of mainstream programming languages over the past 20 years is overwhelmingly clear about this.

There's only one language that I know of that's still in popular use and that likes weak typing.

> That depends on the implementation. I would say Perl handles

Yep that's the one.

> There's only one language that I know of that's still in popular use and that likes weak typing.

As much as I wish it wasn't so, I believe PHP sees more activity currently than Perl, and it's also weakly typed.

Javascript itself is weakly typed as well, which is why we're all having this discussion.

I would say that two of the most popular languages (probably the two most popular new languages) of the last two decades have been weakly typed.

That's a fair point, but I think this article shows that weak typing in JS is actively avoided by most people. That's why the article is interesting in the first place. I don't know if PHP heavily relies on weak typing though, I haven't used it in 9-10 years. But JS doesn't rely on it. It's just there, and easily avoided.