简体   繁体   中英

CFor in Haxe using Macros

So, I love macros (yell at me).

I was trying to create a macro in Haxe, which allows me to write the traditional (C++, Java) for-loop and have the same functionality. But I am quite a beginner in Haxe...

Code:

import haxe.macro.Expr;

class Cfor {

    macro public static function cfor(init: Expr, cond: Expr, post: Expr, body: Expr) {
        return macro {
            $init;

            while ($cond) {
                $body;
                $post;
            }
        }
    }

    public static function main() {
            trace("Traced");
            cfor(var i = 0, i < 100, i++, {
                    var x = i * 2;
                    trace(x);
            });     
    }

}

Questions :

  • It already works (that specific test), but it's not that close to the traditional for-loop. How to improve that?
  • Do you have any other improvements (style/functionality) for this code?
  • Is there anything target specific which I should know of about this code?
  • How can I see what this call to cfor expands to?

What about this?

https://gist.github.com/dpeek/7476625

The approach is different (applied to the context) but I think it is a little closer to the desired outcome.

I don't see any potential target specific issues with your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM