News for hackers. Stuff that matters.
Re:How JavaScript engines achieve great performance by skitter on Wednesday November 24, 2021 @03:47PM (#29334949)
> Arrays are covariant in their type param, so I can pass a `Dog[]` into a function that accepts `Animal[]`. If that function adds a `Cat` to the passed array, the compiler is perfectly happy, but we'll see a runtime error.

The same is true for Java arrays. I thought the Liskov substitution principle was tautological when I first heard about it, but it seems like the original designers of Java disagree. Another example (albeit from the standart library, not the typesystem itself) is that if you try to modify a `List` or similar collection, it may throw an `UnsupportedOperationException` because it's immutable - instead of having a superinterface with only non-mutating methods and only implementing that.

Re:How JavaScript engines achieve great performance by ragnese on Wednesday November 24, 2021 @08:51PM (#29337432)
Ugh. You're going to make me defend Java? I'm going to need a drink...

First point: Yes, Java's arrays are broken and it offends me greatly. However, in practice, it's much more common to use Lists, which are not broken. In JavaScript/TypeScript "Array" is the equivalent of Java's List, so if you want a dynamically sized, contiguous, collection, you are stuck with the broken thing.

Then, the other issue is that, despite my comment's wording, this problem isn't ONLY present in Arrays. It's also a problem with objects, and (I assume) every other non-primitive type. See here: https://www.typescriptlang.org/play?#code/MYGwhgzhAECCB2BLAt...

So even a basic object field is broken in TypeScript's type system. What the hell are we supposed to do with this?

I do have thoughts and opinions about Java's Collection interfaces, the immutable implementations, etc, but that feels like a whole other topic.

Re:How JavaScript engines achieve great performance by jakear on Thursday November 25, 2021 @02:21AM (#29338996)
Yes, TypeScript’s type system is unsound. Yes.

Flow handles some of these cases better, but the invisible keyboard of the market has decided that the extra conceptual/syntactical overhead of introducing co/contravariant types is not worth it for these corner cases. You may disagree, that’s fine. But I’d suggest treating the problem as the engineering tradeoff that it is rather than an absolute fact that every type system must not have any holes and those that do are worthless.

Re:How JavaScript engines achieve great performance by ragnese on Thursday November 25, 2021 @10:28AM (#29342134)
The invisible keyboard of the market... guided by millions of dollars from Microsoft, I assume.

> rather than an absolute fact that every type system must not have any holes and those that do are worthless.

Also, don't straw man me, please.

Re:How JavaScript engines achieve great performance by jakear on Thursday November 25, 2021 @11:23AM (#29342591)
> So even a basic object field is broken in TypeScript's type system. What the hell are we supposed to do with this?

> So a hole exists in TypeScript's type system. What the hell are we supposed to do with this?

> So a hole exists in TypeScript's type system. How can this be useful?

> A hole exists in TypeScript's type system, therefore it is not useful.

> Every type system must not have any holes and those that do are worthless.

Where's the straw?

Regardless, the issue you pose is solved via generics, which would be the appropriate TS way to model this: https://www.typescriptlang.org/play?#code/MYGwhgzhAECCB2BLAt...

Similar constructs would solve your proposed Array issues too: https://www.typescriptlang.org/play?#code/MYGwhgzhAECCB2BLAt...

TS is explicitly designed to be easy to apply to existing codebases without terribly much work. This means it must allow unsound code in some cases. However, given it also provides mechanisms to make the code sound, I do not see why this must be a dealbreaker to you. I think if you took the time to learn it you'd find the type system is actually quite powerful.

(Disclaimer work at MSFT on their TS editor)

Re:How JavaScript engines achieve great performance by ragnese on Thursday November 25, 2021 @07:32PM (#29346428)
> Where's the straw?

Seriously? The very first transformation you did was a straw man.

There are two concepts here. Do you understand that two things can have different importance? Do you also understand that some numbers can be larger than others? For example, 10 > 1.

So, "basic object field sub-types" is a pretty damn big error. Furthermore, I didn't say that this one issue made it worthless. I do think TypeScript is nearly worthless, but if this issue were the single issue with TypeScript, I might not say it's worthless. However, we have this issue, which is a big deal, and we have MORE than one of these type issues.

So, the statement "every type system must not have any holes and those that do are worthless" is not even what I think at all. A small, niche, edge case of a type hole would not make a language as shitty as TypeScript, and therefore would not qualify as a worthless type system, automatically. No, it's the fact that TypeScript's type system has MANY (>1) type soundness/correct issues, and some of them are quite serious and fundamental.

Using generics to "fix" the problem is a work around. It does not somehow make TypeScript's compiler correct. The fact is that the type system is broken/incorrect. There should be no need to use generics, and generics aren't somehow more correct in a type-theory sense.

> I do not see why this must be a dealbreaker to you.

You don't have to. But, I've had to research strange, inconsistent, incorrect behavior in TypeScript way too many times in the last few months. This is a bad language where a signficant amount of the type system is only giving a false sense of security/correctness.

> I think if you took the time to learn it you'd find the type system is actually quite powerful.

I've spent the last year learning WAY more about TypeScript's type system than I should need to. It isn't powerful at all if it's incorrect. Readonly types don't work, sub-type relationships don't work, generics sometimes don't work right/expectedly- where's the "power" in that?

Re:How JavaScript engines achieve great performance by jakear on Thursday November 25, 2021 @09:20PM (#29346956)
Well we both know there are only two types of languages, those that no one uses and those that people waste their time bitching about online. Glad to see TS is in the second category or else my day job would be useless :)

That ad-homonyms do a good job of convincing me this isn’t a thread worth continuing. I wish you well in finding a powerful, complete, and consistent type system. I’m sure you’ll succeed if you try hard enough.

Re:How JavaScript engines achieve great performance by qsort on Wednesday November 24, 2021 @06:09PM (#29336284)
> The same is true for Java arrays.

This is an unfortunate relic of pre-1.5 Java. The rest of the Collection API types correctly. A type-safe generic alternative is Arrays::setAll.

> if you try to modify a `List` or similar collection, it may throw an `UnsupportedOperationException`

Again, an historical artifact. `List` is from Java 1.2, you would effectively have to deprecate List<T> for that to work.

Re:How JavaScript engines achieve great performance by bobthebuilders on Wednesday November 24, 2021 @07:21PM (#29336855)
Most of these things in TS are due to historical artifacts too. Namely code patterns that would not work well without them.