-
-
Save skial/5395970 to your computer and use it in GitHub Desktop.
Haxe Method Cascades [experiement] - http://www.dartlang.org/articles/m1-language-changes/#cascades
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 ; | |
| class A { | |
| public function new() { | |
| } | |
| public function some() { | |
| } | |
| public function thing() { | |
| } | |
| } |
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
| -D debug | |
| -D no-copt | |
| -cp src | |
| -main Main | |
| -js bin/example.methodCascades.js |
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 ; | |
| @:build( MyMacro.build() ) | |
| class Main { | |
| public static function main() { | |
| var a = new A(); | |
| _.some(); | |
| _.thing(); | |
| _. | |
| } | |
| } |
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 example.methodCascades; | |
| import haxe.macro.Compiler; | |
| import haxe.macro.Type; | |
| import haxe.macro.Expr; | |
| import haxe.macro.Context; | |
| /** | |
| * ... | |
| * @author Skial Bainn | |
| */ | |
| class MyMacro { | |
| public static function build() { | |
| var fields = Context.getBuildFields(); | |
| for ( field in fields ) { | |
| switch ( field.kind ) { | |
| case FFun( f ): | |
| f.expr = find( f.expr ); | |
| case _: | |
| } | |
| } | |
| return fields; | |
| } | |
| private static var prev:Var = null; | |
| private static function find(e:Expr):Expr { | |
| var result = e; | |
| switch ( e.expr ) { | |
| case EBlock( exprs ): | |
| var new_exprs = []; | |
| for ( expr in exprs ) { | |
| new_exprs.push( find( expr ) ); | |
| } | |
| result = { expr:EBlock( new_exprs ), pos:e.pos }; | |
| case ECall( expr, params ): | |
| result = { expr:ECall( find( expr ), params ), pos:e.pos }; | |
| case EField( expr, field ): | |
| result = { expr:EField( find( expr ), field ), pos:e.pos }; | |
| case EConst( constant ): | |
| var value = null; | |
| switch ( constant ) { | |
| case CIdent( s ): | |
| value = s; | |
| case _: | |
| value = null; | |
| } | |
| if ( value != '_' ) { | |
| result = { expr:EConst( constant ), pos:e.pos }; | |
| } else { | |
| result = { expr:EConst( CIdent( prev.name ) ), pos:e.pos }; | |
| } | |
| case EVars( vars ): | |
| prev = vars[0]; | |
| result = { expr:EVars( vars ), pos:e.pos }; | |
| case EDisplay(e1, isCall): | |
| // Thanks @simn - https://github.com/HaxeFoundation/haxe/issues/1704 | |
| result = { expr: EDisplay(find(e1),isCall), pos: e.pos }; | |
| case _: | |
| trace( e ); | |
| } | |
| return result; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment