Why don't more languages offer flow typing?
Hafiz writes:<p>&gt; <i>almost every enterprise Java codebase I&#x27;ve had to work with has observed this pattern of testing whether an object is an instance of particular subclass, and using that to drive further computation. Even in the presence of such tests, Java demands that the author cast their objects appropriately. Of course you can always introduce an intermediate variable after the test, but I argue that this is still too much—upon verifying the instance of an object, the type system should be smart enough to update the object&#x27;s type appropriately!</i><p>Because Java considers null to inhabit every type, in a Java project a few years ago, I handled this in a dynamic_cast-like way, as follows (abbreviated):<p><pre><code>    public abstract class Security {     &#x2F;&#x2F; ...
        public Stock asStock()   { return null; }
        public Future asFuture() { return null; }
    }

    public class Stock extends Security { &#x2F;&#x2F; ...
        public Stock asStock() { return this; }
    }

    public class Future extends Security {
        public final SecurityTable factory;
        public final String exchange, symbol; &#x2F;&#x2F; ...
        public Future asFuture() { return this; }
    }
</code></pre>
This allows you to write relatively uncluttered type-dispatching downcasting code like this:<p><pre><code>            public boolean contains(Security sec) {
                Future f = sec.asFuture();

                return f != null
                       &amp;&amp; SecurityTable.this == f.factory
                       &amp;&amp; f.symbol.equals(symbol);
            }
</code></pre>
Of course this violates the open-closed principle (the abstract base class should be closed for modification) and official OO doctrine is that if you want different behavior for different subclasses you should put that behavior into a method that gets overridden by the subclasses, not in a conditional that attempts a downcast, so we did that a lot more often.  But I found it a pleasant solution to the problem in the context of this Java project.<p>Of course it doesn&#x27;t help in languages without implicit nullability, like TypeScript, which would ideally be all statically-typed languages.<p>— ⁂ —<p>The big difficulty with flow typing is, as I see it, not that it clashes with nominal typing; it&#x27;s that it incorporates your <i>compiler&#x27;s</i> static control flow analysis capabilities into your <i>language&#x27;s</i> type system.  Consider Hafiz&#x27;s Java example:<p><pre><code>        if (node instanceof DomNode.Element) {
            Layout layout = ((DomNode.Element) node).layout;
            return new RenderNode.Styled(layout, &#x2F;* ... *&#x2F;);
        }
        return new RenderNode.Noop(&#x2F;* ... *&#x2F;);
</code></pre>
It is entirely reasonable to request that the type system handle this and allow you to write:<p><pre><code>        if (node instanceof DomNode.Element) {
            Layout layout = node.layout;
            return new RenderNode.Styled(layout, &#x2F;* ... *&#x2F;);
        }
        return new RenderNode.Noop(&#x2F;* ... *&#x2F;);
</code></pre>
But how about this?  Now we&#x27;re no longer inside the static extent of the `if`, and we&#x27;re depending on the compiler to recognize the unconditional early return:<p><pre><code>        if (!(node instanceof DomNode.Element)) {
            return new RenderNode.Noop(&#x2F;* ... *&#x2F;);
        }
        Layout layout = node.layout;
        return new RenderNode.Styled(layout, &#x2F;* ... *&#x2F;);
</code></pre>
How about constant folding and partial evaluation?<p><pre><code>        if (1 == 0 || node instanceof DomNode.Element) {
            Layout layout = node.layout;
            return new RenderNode.Styled(layout, &#x2F;* ... *&#x2F;);
        }
        return new RenderNode.Noop(&#x2F;* ... *&#x2F;);
</code></pre>
Do we want to skip type checking entirely for dead code?  Straightforwardly flow typing gives us that the type of every variable inside unreachable code is void, the uninhabited type, so any operation whatsoever on it is type-valid.  Do we want the compiler to accept code like this?<p><pre><code>        if (1 == 0) {
            Layout layout = node.layout + node &#x2F; node;
            return new RenderNode.Styled(layout, &#x2F;* ... *&#x2F;);
        }
        return new RenderNode.Noop(&#x2F;* ... *&#x2F;);
</code></pre>
And of course doing control-flow analysis precisely isn&#x27;t feasible due to the halting problem; you need to do some conservative approximation.<p>So, if you want your programs to be portable from one version of the compiler to the next, somewhere you need to write down precisely <i>what</i> conservative approximation you&#x27;re using for control-flow analysis, and in particular what you&#x27;re <i>not</i>.