Skip to content

Instantly share code, notes, and snippets.

@nyuichi
Created November 11, 2012 17:57
Show Gist options
  • Select an option

  • Save nyuichi/4055705 to your computer and use it in GitHub Desktop.

Select an option

Save nyuichi/4055705 to your computer and use it in GitHub Desktop.
enhance type inference of conditional expression
class A {}
class B extends A {}
class C extends A {}
interface I {}
class X implements I {}
class Y implements I {}
class Z extends Y {}
class _Main {
static function main(args : string[]) : void {
var variant_value = 123 as variant;
var a = (true)? 1 : 1.0; // number
var b = (true)? "asdf" : [ "a", "b"][0]; // Nullable.<string>
var c = (true)? variant_value : 1; // variant
var d = (true)? new A : new B; // A
var e = (true)? new A : null; // A
var f = (true)? 1 : null; // Nullable.<number>
var g = (true)? "a" : null; // Nullable.<string>
var h = (true)? new B : new C; // A
var i = (true)? new X : new Y; // I
var j = (true)? new X : new Z; // I
var k = (true)? new A : new X; // Object
// for now avoids returning variant type as a join of a primitive and an object. (but it can be easily changed)
// var l = (true)? 1 : new A; // error
var m = (true)? function (a : A) : X { return new X; } : function (a : A) : X { return new X; }; // (A) -> X
// currently calculation of the joins of function types is not supported.
// var n = (true)? function (a : A) : Y { return new Y; } : function (b : B) : X { return new X; }; // (B) -> X
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment