简体   繁体   English

使用Macros在Haxe中的CFor

[英]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. 我试图在Haxe中创建一个宏,它允许我编写传统的(C ++,Java)for循环并具有相同的功能。 But I am quite a beginner in Haxe... 但我是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. 它已经可以工作了(那个特定的测试),但它并不接近传统的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? 我怎样才能看到这个对cfor的调用扩展到了什么?

What about this? 那这个呢?

https://gist.github.com/dpeek/7476625 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. 我没有看到您的代码存在任何潜在的目标特定问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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