Constant page does not describe 'static inline var' as creating a constant.
EricBishton opened this issue · 2 comments
Regarding page: https://haxe.org/manual/expression-constants.html
Constant expressions are required for default arguments in function parameters. The compiler allows variables declared as 'static inine var' and allowing some level of expression to be used in that position.
For example, the following code compiles, but you wouldn't know it from reading the manual page for either constants or default parameters (which only mentions that the default value must be a constant):
class Test {
static inline var myConstant : Int = 4 + 3; // Expression!
static inline var myStr = "Something";
static inline var myInt = Std.int(1.2);
static inline var myComposite = "1" + "2";
@:pure
public static function concat(a:String, b:String) : String {
return a + b;
}
public static function test(param : Int = myConstant) {return param;}
public static function test2(param : Int = myInt) {return param;}
public static function test3(param : String = myStr) {return param;}
//public static function test4(param : String = myComposite) {} // Only this one causes an error.
static function main() {
trace(test());
trace(test2());
trace(test3());
//trace(test4());
}
}
Additionally, the manual page contains the text: "All constants are literals, except for argument-less enum constructors:", which is incorrect (because an expression is not defined as a literal, even though it is allowed).
Please also document what expressions are considered valid when creating a constant.
In fact, these also are allowed as constants:
static inline var myParenthesizedInt = Std.int((1+2) * ((4/2) + 3));
static inline var myString2 = Std.string(myParenthesizedInt);
According to @Simn: Some functions are optimized in the compiler if they
have static arguments: Type.enumIndex, String.fromCharCode,
Std.string, Std.int, Math.ceil, Math.floor, Std.is.