Created
September 14, 2012 16:07
-
-
Save andyli/3722907 to your computer and use it in GitHub Desktop.
new haxe metadata proposal
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
| package mylib; | |
| //extends or implements a special class/interface | |
| class Limit implements haxe.rtti.Meta { | |
| //the 1st param tell what kinds of field it applies, just like "using" | |
| //Used as @limitMinMax(0, 100) or @mylib.Limit.limitMinMax(0, 100) | |
| static public function limitMinMax(field:Float, min:Float, max:Float):Bool { | |
| //return whether the input is valid | |
| return min <= max; | |
| } | |
| //macro metadata! | |
| //Able to take Expr as param. | |
| //Used as @:limitExpr(!Math.isNaN(_)) or @:mylib.Limit.limitExpr(!Math.isNaN(_)) | |
| @:macro static public function limitExpr(field:Float, e:haxe.macro.Expr):Bool { | |
| //optionally process the field using some haxe.macro.Context | |
| return true; | |
| } | |
| } |
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
| using mylib.Limit; | |
| class Test | |
| { | |
| @limitMinMax(0, 100) public var field1:String; //error: limitMinMax only applies to Float | |
| @limitMinMax public var field2:Float; //error: limitMinMax takes two arguments | |
| @limitMinMax(10, 0) public var field3:Float; //error: the function returns false | |
| @limitMinMax(0, 10) public var field3:Float; //alright! | |
| @mylib.Limit.limitMinMax(0, 10) public var field3:Float; //alright, with fully qualified name! | |
| public static function main() | |
| { | |
| new B(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment