Get value from enum

Hello All

how to get value from enums? i’m trying to use svg library and get Path segments.
svg objects are organised inside this lib as enum:

enum DisplayElement {
       DisplayPath(path:Path);
       DisplayGroup(group:Group);
       DisplayText(text:Text);
}

and i need to get that Path instance. i ended u with following code:

var p:Path = switch(testSvg.findGroup("layer1").children[0]) {
    case DisplayGroup(group):
    case DisplayPath(path): path;
    case DisplayText(text):
}
		
for(segment in p.segments){
    trace(segment);
}

it works, but it looks to complicated to use switch if i know exactly what to expect.
may be there is another way to get Path instance from this Enum?

1 Like

I don’t think Haxe has any built-in way to do this, but you could try using this macro which shortens it a lot: https://gist.github.com/nadako/3db9c067a4e93d64d1f4

Other than that, you don’t need to have both DisplayGroup(group) and DisplayText(text) in your enum if you only care about the path, you can make the switch exhaustive by adding a single default: / case _:

Usually you use a switch, or you might be able to use something like Type.getEnumParameters

what do you mean by getEnumParameters? i know it in c#, but have not found anything in haxe.

Haxe has a class called Type, I think there is a function called Type.getEnumParameters (enumValue)

1 Like

you are right! that is Type.enumParameters