Last active
December 14, 2015 21:39
-
-
Save deltaluca/5152448 to your computer and use it in GitHub Desktop.
typed flags haxe.
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
abstract Flags(Int) to Int { | |
public static var A(default,never):Flags = 1; | |
public static var B(default,never):Flags = 2; | |
public static var C(default,never):Flags = 4; | |
@:from inline static function fromInt(x:Int) return new Flags(x); | |
inline function new (f:Int) this = f; | |
@:op(A|B) inline public static function join(a:Flags, b:Flags):Flags { | |
var _a:Int = a; | |
var _b:Int = b; | |
return new Flags(_a|_b); | |
} | |
public function toString() { | |
return "F"; | |
} | |
} | |
class Main { | |
static function main() { | |
$type(Flags.A); // Warning: Flags | |
$type(Flags.B); // Warning: Flags | |
$type(Flags.A|Flags.B); // Warning: Flags | |
trace(Flags.A); // 1 | |
trace(Flags.B); // 2 | |
trace(Flags.A|Flags.B); // 3 | |
var a = Flags.A; | |
$type(a); // Flags | |
trace(a); // 1 | |
// var b = new Flags(1); //<-- Error | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment