as3hx icon indicating copy to clipboard operation
as3hx copied to clipboard

Support AS3 conditional compilation, aka compiler constants / variables

Open jcward opened this issue 10 years ago • 2 comments

AS3 / mxmlc supports conditional compiler variables / expressions: http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf69084-7abd.html

(Issue #45 mentions these but only one specific use case.)

You can use booleans to conditionally include entire blocks of code:

CONFIG::FOO {
  ...
}

The doc says you can tag definitions at the package-level (without curly braces):

CONFIG::debugging
public class MyButton extends Button {    
...

CONFIG::release
public class MyButton extends Button {    
...

I know that you can also enclose those definitions in `{}.

You can also make conditional things like import statements and inline classes, though the latter could be rare:

CONFIG::AIR { // This import doesn't exist for non-AIR target
  import flash.desktop.NativeApplication;
}

You can use constants, both booleans and Strings:

if (CONFIG::FOO) { ... }

var host:String = CONFIG::HOSTNAME;
trace("The host is: "+host);

The docs state you can pass expressions "a > b", though this one seems odd, I can't think of a good use case / reason you'd do that.

Syntax: Note that I've used the prefix CONFIG -- but I believe any / multiple prefixes can be defined. Uppercase prefix is convention, but I'm not sure it's required. The key name case doesn't matter.

Some of the use cases translate easily, but others are more complicated:

  • PREFIX::KEY { ... } could map to Haxe's conditional compilation #if PREFIX_KEY ... #end, with perhaps a special case prefix/key mapping to #if debug (programmable? default?)
  • Constants could be mapped to a file of Static public inlines with unknown type and value? It wouldn't compile, but it'd be tidy / obvious. Or perhaps a macro that converts Haxe -D definitions to static inline constants (string or boolean).

jcward avatar Nov 12 '15 17:11 jcward

Interesting, the conditional blocks of code at least runs through the converter:

CONFIG::DEBUG { trace("In debug mode"); }

becomes:

/* AS3HX WARNING namespace modifier CONFIG::DEBUG */{trace("In debug mode");
}

IMO, ideally this would convert to:

#if CONFIG_DEBUG
  trace();
#end

Or, for the special case, CONFIG_DEBUG maps to just debug but not in all cases.

jcward avatar Nov 12 '15 17:11 jcward

FYI, perhaps useful for working around this, I used the following to recursively replace BUILD::DEMO (a boolean compiler constant) with Constants.BUILD_DEMO in all files:

grep -rl BUILD::DEMO . | xargs sed -i 's,BUILD::DEMO,Constants.BUILD_DEMO,g'

This at least makes them parseable, and I can build a Constants class to provide the constant / inline values.

jcward avatar Jun 23 '16 03:06 jcward