Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
741 views
in Technique[技术] by (71.8m points)

compilation - Is there a compiler preprocessor in 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)) 

or :

#define NumType double
#define NumTypeZero 0.0

// used with :
NumType myNum = NumTypeZero;

or :

#define DEBUG 

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

)

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).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Basically what the other guys said...

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.

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.

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

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

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.

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>".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...