Skip to content

Instantly share code, notes, and snippets.

@kLabz
Created May 2, 2019 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kLabz/46cd0d227bd3c0e2da207f9c05e50f4a to your computer and use it in GitHub Desktop.
Save kLabz/46cd0d227bd3c0e2da207f9c05e50f4a to your computer and use it in GitHub Desktop.
Extends<T>
class Test {
static function main() {
var a:Extends<A> = extended({a: "hello", b: "world"});
$type(a); // Extends<A>
trace(haxe.Json.stringify(a));
a.a = "bye";
// a.a = 42; // Int should be String
// a.b = "nope"; // Extends<A> has no field b
trace(haxe.Json.stringify(a));
// --------
var props:WrapperProps = {
whatever: true,
AProps: extended({a: "hello", b: "world"})
};
trace(haxe.Json.stringify(props));
}
static function extended<Base, T:Base>(obj:T):Extends<Base>
return cast (obj :Extended<Base, T>);
}
typedef A = {
var a:String;
}
typedef WrapperProps = {
var whatever:Bool;
var AProps:Extends<A>;
}
typedef Extends<T> = T;
typedef Extended<T, TExtended:T> = TExtended;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment