Comment

Comment by curryhoward

Just as an example, this program type checks in TypeScript but crashes at runtime:

    class Dog {
    }

    class Greyhound extends Dog {
        doGreyhoundThing(): void {
            console.log("I am a greyhound!");
        }
    }

    class Poodle extends Dog {
        doPoodleThing(): void {
            console.log("I am a poodle!");
        }
    }

    function f(g:(Dog) => void) : void {
        let hound: Greyhound = new Greyhound();
        g(hound);
    }

    function h(p: Poodle): void {
        p.doPoodleThing();
    }

    f(h);
`f(h);` would be a type error if function types were contravariant in their argument types. TypeScript made the unsound choice to let function types be bivariant in their argument types, which the authors claim is justified for practical reasons. More info here: https://github.com/Microsoft/TypeScript/wiki/FAQ#why-are-fun...
parent curryhoward 9 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton