Created
October 1, 2012 17:06
-
-
Save puffnfresh/3813040 to your computer and use it in GitHub Desktop.
Covariant arrays. Blurgh.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Compiles fine even though the code is broken. | |
// This is because arrays are covariant and mutable. | |
// | |
// TypeScript team: Please fix either of those things. Preferably mutability :) | |
class Animal {} | |
class Snake extends Animal { | |
slither() { | |
alert("Slithering!") | |
} | |
} | |
class Horse extends Animal {} | |
var a: Snake[] = [new Snake()] | |
var b: Animal[] = a | |
b[0] = new Horse() | |
var notASnake: Snake = a[0] | |
notASnake.slither() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment