Re:Why don't more languages offer flow typing?
by yxhuvud on Monday April 04, 2022 @01:55PM
(#30909815)
Why should type narrowing be limited to just case, and not both case and if? I'd argue that both of these are examples of flow typing.
Re:Why don't more languages offer flow typing?
by colonwqbang on Monday April 04, 2022 @05:02PM
(#30911889)
My point was that if your language has a dedicated facility for case analysis, you don't really need to use if statements for that purpose. You just use case. That's one answer to "why don't more languages have flow typing".
Re:Why don't more languages offer flow typing?
by ljloisnflwef on Monday April 04, 2022 @07:08PM
(#30913160)
Yes and no.
Consider predicate types, or first-class null safety through unions.
If statements introduce propositions, which type systems could take advantage.
example:
let idx = randomInt();
if (0 <= idx && idx < arr.length) { // proposition introduced
// idx now has type: int where 0 <= idx && idx < arr.length
// irrefutable proof of safe indexing by type propositions
let value = arr[idx];
}
There is a language construct which is purpose built for unpacking sum types: The case expression (sometimes called match expression)
Inside each branch, we have access to a value of the more specific type.