4 ms·
This 'flow typing' / TypeGuard approach is really awesome and probably the best part about languages like typescript/mypy. I wish that other languages would fol
by staticassertion 4y ago
This 'flow typing' / TypeGuard approach is really awesome and probably the best part about languages like typescript/mypy. I wish that other languages would follow suit so that I can incrementally narrow the types in my program through conditionals.
- leni536 4y agoWhile it is cool and useful, it is unsound. https://mypy-play.net/?mypy=latest&python=3.10&flags=strict&gist=6e2532267d6ecca04896c1223473b3f9 https://mypy-play.net/?mypy=latest&python=3.10&flags=strict&... https://www.online-python.com/V5YRZJAWlk https://www.online-python.com/V5YRZJAWlk
- staticassertion 4y agoNice, that's a cool example. But that's not fundamental to the concept of flow typing, it's just a mypy limitation.
- planede 4y agoAnd a typescript limitation? https://www.typescriptlang.org/play?#code/MYGwhgzhAEDC0G8BQ1XQB4C5oDsCuAtgEYCmATtAD654gjQC80ADANxIC+SSAZnjsAAuASwD2OaD1GiAFMGywAlNgBuo4QBNEKNMAB06RjTrsuvfkLESiYMnIXKaxctrTRhPaHIOMGTfHSKrm5oZCSCeGQSbDqoXG5SssCK7G5hEVHQ+uim3CDhGNj4zhRMNnY4JADucDKKKdzA4hCi+XogogDmMugpQA https://www.typescriptlang.org/play?#code/MYGwhgzhAEDC0G8BQ1...
- staticassertion 4y agoYes, it seems that the issue is essentially one of binding the type to a scope but then mutating within that scope. The type system could just "look" for those mutations, or the language could add mutability to the type system.
- planede 4y ago> The type system could just "look" for those mutations That's a very big "just". Typescript and mypy don't look through function call boundaries, doing so would open a lot of problems, I believe. > or the language could add mutability to the type system. I agree there, the core issue is that the call to `foo` leaks a an object where the member is rebindable through assignment, but the "flow typing" assumes that doesn't happen. Ideally such a call should be a barrier for flow typing, unless there was a way to specify functions that don't modify their parameter this way (readonly parameters?). A similar problem but with a global instead: https://www.typescriptlang.org/play?#code/DYUwLgBAlhBcEDsCuBbARiAThAPopwwEAvBACwBMA3AFA0BmSCAxmFAPYIT3vsAUASngA3dlAAmEAN40Ic6CXyFaAXzqMWbThDQBDTIPjJ0WabPlR6EPjGJ2lwAWfkuImcEkxcADLRdqXHn4BP3l3ME8uKFU6UEgADyNUDGxSPQMQumZOAGd2UAA6YHYAcz54kKA https://www.typescriptlang.org/play?#code/DYUwLgBAlhBcEDsCuB... This should be handled differently. Every function should be assumed to modify globals, unless they can be marked to be pure.
- staticassertion 4y agoYeah, that is definitely a big "just" - it's basically whole program type inference, which I expect to be very messy in Python, if not intractable. > unless there was a way to specify functions that don't modify their parameter this way (readonly parameters?). Right, so in a language with typed mutability (ie: Rust) or total immutability flow typing would be a lot easier to implement without these footguns. Python is such an insane language, I'm not surprised to see that you can subvert the type checker like this.
- IshKebab 4y agoIn fairness, Dart took the fully-sound approach and while it is clearly better, it does make dealing with class member variables a massive pain for similar reasons. Also clearly a bit of unsoundness is way better than just no type annotations at all, which is the alternative.
- planede 4y agoInteresting. It looks like it just doesn't allow narrowing types of class members and globals, sidestepping this whole issue. I like the Dart approach here. Users of the language can always bind a local variable to the thing you want a narrower type for, it's not the big of a hassle.
- IshKebab 4y agoIt is quite a hassle when every method looks like void foo() { var local_a = this.a; var local_b = this.b; ... this.a = local_a; this.b = local_b; } But I'd say it's still worth it for the soundness. And I have confidence they'll come up with some improvement. Maybe a way to opt out of setters/getters.