News for hackers. Stuff that matters.
Re:Why don't more languages offer flow typing? by simulate-me on Monday April 04, 2022 @12:12PM (#30908337)
Another very common case in TypeScript (and other languages) is union types. Other languages have constructs for checking the underlying type of a union type directly, but typescript is smart enough to figure it out based off of the properties of the constituent types and what you've checked so far in your code.

e.g.

    interface A { type: 'a' }
    interface B { type: 'b' }
    type C = A | B;

    const c: C = ...;
    if (c.type === 'a') { /* c is of type A here */ }
Re:Why don't more languages offer flow typing? by jakelazaroff on Monday April 04, 2022 @01:15PM (#30909241)
That's basically case 1, right? The compiler knows which type is being used at each call site, so it can generate a separate function for each type in the union and eliminate the type check/dead branches.

I guess the exception would be if you have a non-homogenous array that you try to map over. In that case there's probably no way around boxing the values.

Re:Why don't more languages offer flow typing? by xg15 on Monday April 04, 2022 @02:48PM (#30910559)
For consts and function arguments yes, I think. But you could have some mutable variable or field whose value depends on runtime state. In that case, you could have an actual polymorphic type.
Re:Why don't more languages offer flow typing? by epolanski on Monday April 04, 2022 @05:46PM (#30912374)
But the previous example isn't that case.

if (c.type === 'a') { /* c is of type A here */ }

This is dynamic typing, this code is checked at runtime, and it's leveraged statically.

Re:Why don't more languages offer flow typing? by xg15 on Monday April 04, 2022 @06:39PM (#30912907)
Yes, that's correct. In the GP example, c was const, so the type can be determined at compile time.
Re:Why don't more languages offer flow typing? by simulate-me on Monday April 04, 2022 @07:55PM (#30913520)
I was writing TypeScript, where const just means the variable is not reassigned. For instance this is valid:

    const c: C = Math.random() < 0.5 ? { type: 'a' } : { type: 'b' }.
Re:Why don't more languages offer flow typing? by xg15 on Tuesday April 05, 2022 @01:22AM (#30915556)
Ah, I'm sorry. You're right of course!