Hacker News new | ask | show | jobs
by colonwqbang 1544 days ago
Doesn't "flow typing" seem more like a bandage for the if-statement in languages that have nothing better.

There is a language construct which is purpose built for unpacking sum types: The case expression (sometimes called match expression)

  case resp of
    Views n -> ...
    Error e -> ...
Inside each branch, we have access to a value of the more specific type.
2 comments

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.
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".
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];
  }