Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constant page does not describe 'static inline var' as creating a constant. #430

Open
EricBishton opened this issue Aug 18, 2019 · 2 comments

Comments

@EricBishton
Copy link
Member

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.

@EricBishton
Copy link
Member Author

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

@EricBishton
Copy link
Member Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant