-x Main |
import haxe.macro.Expr; | |
class FTW | |
{ | |
public static function build() | |
{ | |
return haxe.macro.Context.getBuildFields().map(transformField); | |
} | |
static function transformField(field:Field) | |
{ | |
switch (field.kind) | |
{ | |
case FFun(f): transformExpr(f.expr); | |
default: | |
} | |
return field; | |
} | |
static function transformExpr(expr:Expr) switch (expr) | |
{ | |
case macro @for($init, $cond, $incr) $block: | |
transformExpr(block); | |
expr.expr = makeLoop(init, cond, incr, block).expr; | |
default: | |
haxe.macro.ExprTools.iter(expr, transformExpr); | |
} | |
static function makeLoop(init:Expr, cond:Expr, incr:Expr, block:Expr) return macro | |
{ | |
$init; | |
while ($cond) | |
{ | |
$block; | |
$incr; | |
} | |
} | |
} |
This comment has been minimized.
This comment has been minimized.
Very smooth |
This comment has been minimized.
This comment has been minimized.
All credit goes to @Simn for pointing out it was possible. Magical. |
This comment has been minimized.
This comment has been minimized.
while it seems nice. Still trying to understand Macro. Are all the code in FTW just boilerplate for any macro? |
This comment has been minimized.
This comment has been minimized.
Is this "for the win", or "for the whiners"? |
This comment has been minimized.
This comment has been minimized.
it's not a boilerplate. this is a build-macro, its entry point is the "build" function (as specified in @:build meta), as a build macro it should return a list of generated fields for given class. what this particular macro does is iterate over already defined fields and maps them to the transformField field, which in calls transformExpr for the expression in every function field (TFun) the transformExpr matches given expression for a if matching for this is a form of meta-programming if you like |
This comment has been minimized.
This comment has been minimized.
While technically pretty cool, current implementation of
at the price of loosing ability to use But, as said, very interesting. Now I'm starting to get curious whether it's possible to autoinsert @:build into all classes on project level. |
This comment has been minimized.
This comment has been minimized.
@nadako thanks for the explanation. Never knew Macro is a meta programming thing. |
This comment has been minimized.
This comment has been minimized.
@YellowAfterlife we could add a helper that allowed you to do: |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Possible workaround allowing both continue and break:
(Untested.) |
This comment has been minimized.
This comment has been minimized.
Made a better version: |
This comment has been minimized.
This comment has been minimized.
player-03: Your version combines duplicating code (slightly larger output size) with creating anonymous functions (or even whole classes, if it's C#/Java) on most targets. It's an arguable advancement. Example: http://try.haxe.org/#7F556 |
This comment has been minimized.
This comment has been minimized.
A year and a half later, I finally noticed your comment. Sorry for the late response! Anyway, I figured out that you meant to include a Whichever it was, I've fixed the issue. My code checks for the existence of |
This comment has been minimized.
this is just too epic