HaxeFoundation / haxe-evolution

Repository for maintaining proposal for changes to the Haxe programming language
111 stars 58 forks source link

Support default arguments for enum constructors. #116

Closed EliteMasterEric closed 7 months ago

EliteMasterEric commented 7 months ago

I think this issue is pretty well summarized by the following code snippets.

/**
 * The syntax below has intuitive behavior, but is not supported Haxe syntax.
 */
enum TestEnum {
    INTEGER(value:Int = 0);
}

class Test {
  static function main() {  
    var value:TestEnum = INTEGER();
    // NOTE: It'd be nice to also be able to do this as long as all arguments are nullable or have defaults.
    var value2:TestEnum = INTEGER;

    switch(value) {
        case INTEGER(innerValue):
            trace('Inner value was: $innerValue');
    }
  }
}
/**
 * The syntax below is valid syntax but requires handling default values where they are processed.
 */
enum TestEnum {
    INTEGER(?value:Int);
}

class Test {
  static function main() {  
    var value:TestEnum = INTEGER();

    switch(value) {
        case INTEGER(innerValue):
            if (innerValue == null) {
                innerValue = 0;
            }
            trace('Inner value was: $innerValue');
    }
  }
}

In my opinion, the improvement in code quality is substantial, the new behavior is obvious, and there are no issues with backwards compatibility since the syntax is not previously valid.

I'll open this issue up as a place for feedback and comments before writing up a proper evolution proposal.

Simn commented 7 months ago

See #27.