Haxe Enum Default Settings

Can I use the enumdefault options in Haxe?

I get an error Parameter default value should be constant

enum AnEnum {
    A;
    B;
    C;
}

class Test {
    static function main() { 
        Test.enumNotWorking();
    }
    static function enumNotWorking(?e:AnEnum = AnEnum.A){}
}

Try haxe link

+4
source share
2 answers

Exists if you want to use enum abstracts (enumerations at compile time, but at runtime of a different type):

@:enum
abstract AnEnum(Int)
{
    var A = 1;
    var B = 2;
    var C = 3;
}

class Test3
{
    static function main()
    {
        nowItWorks();
    }

    static function nowItWorks(?param=AnEnum.A)
    {
        trace(param);
    }
}

There is nothing special about the values โ€‹โ€‹that I selected, and you can choose a different type (string or a more complex type) if it suits your use case better. You can treat them the same way as regular enumerations (for switch statements, etc.), but note that when you trace it at runtime, you will get "1", not "A".

: http://haxe.org/manual/types-abstract-enum.html

+3

, Haxe .

-, , , :

http://old.haxe.org/ref/enums#using-enums-as-default-value-for-parameters

:

static function enumNotWorking(?e:AnEnum){
  if (e==null) e=AnEnum.A;
}

.

+2

All Articles