Dart 中有编译器预处理器吗? [英] Is there a compiler preprocessor in Dart?

查看:41
本文介绍了Dart 中有编译器预处理器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于在启动 dart 应用程序之前需要进行编译,我想知道编译器预处理器是否可用,或者计划在不久的将来用于 Dart.
到目前为止,我在网上/飞镖网站内的搜索均未成功.

Since a compilation is required before launching a dart application, i wonder if compiler preprocessor is available, or is scheduled in a near future for Dart.
My searches on the net / inside the dart site have proven unsuccessful so far.

(通过预处理器,我的意思是:

( By preprocessor, i mean something like :

#define max(A,B)    ( (A) > (B) ? (A):(B)) 

或:

#define NumType double
#define NumTypeZero 0.0

// used with :
NumType myNum = NumTypeZero;

或:

#define DEBUG 

// use
#ifdef DEBUG
   print('var1 : $var1, var2:$var2, ...');
#endif

)

我想知道为什么没有准备好预处理器,因为从那时起我们似乎接近"了:
- Dart 必须扫描文件中的库依赖项才能以正确的顺序加载库.
- Dart 编辑器还会扫描文件以进行语法、类型检查和其他检查.
- 可以在编辑器中启动一些自动文件处理(我找不到有价值的链接,如果你有的话,请告诉我).

Edit : I wonder why there's not allready a pre-processor because it seems we are 'near' from that point :
- Dart has to scan the files for library dependencies to load the libraries in the right order.
- Dart editor also scan files for syntax, type checking and other checks.
- It's possible to have some automated file processing launched within the editor (i couldn't find a valuable link for this, please let me know if you have one).

推荐答案

基本上其他人说的...

Basically what the other guys said...

如果您使用 dart2js 进行编译,如果 DEBUG 是常量且为 false,则摇树会抛出 if (DEBUG) {} 块内的代码.所以你可以只使用 if 语句.

If you're compiling with dart2js, the tree-shaking will already throw out the code inside an if (DEBUG) {} block, if DEBUG is a constant and false. So you can just use if statements.

您也可以使用 assert() 语句.使用 dart2js 编译生产模式时,断言和传递给它的表达式将被丢弃.

You can also use assert() statements. The assert, and the expression passed to it will be thrown away when compiling with dart2js for production mode.

所以这实际上与您使用 #ifdefs 获得的行为相同 - 您可以将 dart2js 视为您的预处理器 ;)

So this is actually the same behaviour that you'd get with #ifdefs - you can think of dart2js as your preprocessor ;)

我也没有看到您为什么要使用 #defines 而不是常量的任何理由.

I also don't see any reason why you would want to use #defines instead of constants.

如果您想在 DartVM 中运行您的代码,您可以使用 dart2js --output-type=dart 对您的 dart 源进行摇树.

If you want to run your code in the DartVM, you can use dart2js --output-type=dart to do tree-shaking on your dart source.

更新:另见String.fromEnvironment(), bool.fromEnvironment(),以及 int.fromEnvironment().您可以在编译时使用dart2js -D<env-var-name>=<value>"设置这些环境变量.

Update: Also see String.fromEnvironment(), bool.fromEnvironment(), and int.fromEnvironment(). You can set these environment variables when compiling by using "dart2js -D<env-var-name>=<value>".

这篇关于Dart 中有编译器预处理器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆