Using constants as default parameter values ​​in interfaces: IDE is ok, but mxmlc fails?

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

senocular gives a good explanation and maybe a workaround, but I hope for something more elegant (like on the command line).

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 { } } } 
+6
source share
1 answer

According to Senocular, this is all about compilation order. There is no explicit way to establish this order.

You can define built-in constants using the define parameter to avoid this problem.

Another way is to create a library containing constants. Libraries are included in front of 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, enable this library:

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

Remember to recompile the library when changing constants or use a script construct that takes care of this.


A brief comment on the sample code:
The interface does not need to use this constant, it will have any value and will have the same effect on the implementation of classes.

AS3 Programming - 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 must not match.

+2
source

Source: https://habr.com/ru/post/890441/


All Articles