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

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

问题描述

由于在启动dart应用程序之前需要编译,我不知道编译器预处理器是否可用,或者在不久的将来为Dart调度。

我在dart网站上的net /至今证明不成功。

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()语句。 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天全站免登陆