【问题标题】:Access the compilation date during compilation in Haxe在 Haxe 编译期间访问编译日期
【发布时间】:2017-11-26 09:04:33
【问题描述】:

我想知道是否可以访问外部信息——比如编译期间的当前日期。

这样就可以做这样的事情了:

class MyInfo {
    private var buildDate:Int = --- AUTOMATICALLY INSERT THE CURRENT UNIX DATE TIME HERE ---;

    public function getInfo():String { // example usage
        return "This library was compiled the " + buildDate;
    }
}

我考虑在编译 bat/sh/make 文件中访问这些信息,然后也将其传递给编译器。 (类似于“-D”的东西。)但是,Haxe 编译器似乎不支持如下参数:

haxe --main MyInfo --js test.js -SOMEARG date=$(date)

这样我以后就可以使用变量日期的内容...

【问题讨论】:

    标签: macros haxe


    【解决方案1】:

    这可以通过宏来完成(在编译时执行的代码)。

    食谱here 中介绍了您的日期示例。 您可以在in the haxe manualcookbook 中找到有关宏的更多信息。

    编辑: 最小的例子:

    class Test {
      public static function main() {
        trace(getBuildTime());
      }
    
      public static macro function getBuildTime() {
        var buildTime = Math.floor(Date.now().getTime() / 1000);
    
        return macro $v{buildTime};
      }
    }
    

    时间将在编译时计算。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-27
      • 1970-01-01
      • 1970-01-01
      • 2015-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多