Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using constants as default parameter values in interfaces: IDE okay but mxmlc fails?

This code seems to compile fine in the IDE, but the command-line compiler (SDK 4.5 mxmlc.exe) reports "Parameter initializer unknown or is not a compile-time constant."

senocular gives a good explanation and a maybe-workaround, but I'm hoping for something more elegent (like a command-line instruction).

package {
    public class Constants {
        public static const CONSTANT : int = 0;
    }
}


package {
    public interface IInterface {
            function foo( param : int = Constants.CONSTANT ) : void;
    }
}

package
{
    public class Concrete implements IInterface
    {   
            public function foo(param:int=Constants.CONSTANT):void
            {        
            }
    }
 }
like image 962
Richard Haven Avatar asked Jun 13 '11 20:06

Richard Haven


1 Answers

According to Senocular, it's all about the compilation order. There's no explicit way to set this order.

You could define inline constants using the define compiler option to avoid this problem.

Another way would be to create a library containing the constants. Libraries are included before user classes. To create a library use the component compiler:

compc -output lib\Constants.swf -source-path src -include-classes Constants

When compiling the application, include that library:

mxmlc -include-libraries lib\Constants.swf -- src\Main.as

Just don't forget to recompile the library when the constants change, or use a build script that takes care of that.


A short comment on the example code:
The interface doesn't need to use that constant, any value will do and have the same effect on implementing classes.

Programming AS3 - Interfaces

A method that implements such a function declaration must have a default parameter value that is a member of the same data type as the value specified in the interface definition, but the actual value does not have to match.

like image 186
kapex Avatar answered Nov 02 '22 17:11

kapex