HaxeFoundation / haxe.org-comments

Repository to collect comments of our haxe.org websites
2 stars 2 forks source link

[haxe.org/manual] Enum abstracts #78

Open utterances-bot opened 3 years ago

utterances-bot commented 3 years ago

Enum abstracts - Haxe - The Cross-platform Toolkit

Haxe is an open source toolkit based on a modern, high level, strictly typed programming language.

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

AG-w commented 3 years ago

For anyone want to use enum abstract in any fields you need cast it or compiler won't compile they don't really bother to explain it here so you need be careful

or change enum abstract Textual(String) to enum abstract Textual(String) to String so you don't need cast at all

nadako commented 3 years ago

It is explained here https://haxe.org/manual/types-abstract-implicit-casts.html

AAChartModel commented 3 years ago
enum abstract Textual(String) {
  var FirstCase; // implicit value: "FirstCase"
  var AnotherCase; // implicit value: "AnotherCase"
}

It is perfect solution sample for me!

onehundredfeet commented 1 year ago

Is there a way to get the name given the value? I want an abstract enum based on an Int, but for debugging, I want to print the name.

sayofthelor commented 1 year ago

@onehundredfeet there’s one way i can think of but it’s definitely spaghetti code to the max

i modified some flixel macro code to get key value pairs from an abstract from the direct name of it (here, function name is getMapFromAbstract) which should be able to help here although it’s worth noting this wouldn’t work in hscript or anything like that since it’s a macro

let’s say you had an abstract like this:

abstract SomeAbstract(Int) from Int to Int {
    var ExampleOne = 48;
    var ExampleTwo = 93;
}

in order to get the name of it from a function you could do something like this:

function getSomeAbstractThingName(value:SomeAbstract):String {
    var valMap = MacroTools.getMapFromAbstract(SomeAbstract, true);
    return valMap.get(value);
}

then you could use it like this:

trace(getSomeAbstractThingName(SomeAbstract.ExampleTwo)); // should be “ExampleTwo”
// or
trace(getSomeAbstractThingName(48)); // should be “ExampleOne”

this is a long ass way to do it but it’s the only way i could think of right now, i’ll make a comment if i think of a better way

Sedomanai commented 1 year ago

@sayofthelor Thank you for the example, I'm sure it works. But I'm afraid some of us are just starting out; I can't seem to find "something like MacroTools.getMapFromAbstract(SomeAbstract, true);" anywhere.

danielo515 commented 1 year ago

This may help to understand how this works: https://code.haxe.org/category/macros/enum-abstract-values.html

farteryhr commented 9 months ago

just a nitpick: should we use final instead of var here since they're not "variables", although (yes i've read that) final was introduced in 4.0.0...

c-g-dev commented 8 months ago

Am I missing something? The article doesn't make it clear you can only use constant types for enum abstracts. It sort of gives you the impression that any type can be used and the enum boxing will let you describe an indexed/matchable/typed set of those values, which would be cool.

But trying to do something like this:

class Foo {
    public static final FOO_2 = new Foo(2);
    public function new(i:Int) {}
}

enum abstract MyEAbstract(Foo) {
    var Bar1 = new Foo(1); //not allowed
    var Bar2 = Foo.FOO_2; //still not allowed
}

will only throw "Inline variable initialization must be a constant value". This is even more confusing considering that

enum abstract MyEAbstract(Foo) {}

Doesn't throw a compiler error, despite wrapping a type which apparently could never actually be used, making you think that you are just instantiating the variables the wrong way when you get the "must be a constant value" error message. The only indication that you can't do this is buried 2 levels deep in the vague phrase that "This is similar to accessing variables declared as inline" and then needing to know that inlined variables can strictly only be a small set of primitive types, despite the fact that the "final" keyword exists.

ConfidantCommunications commented 8 months ago

@onehundredfeet @sayofthelor There is also EnumTools.

using haxe.EnumTools;
function test(e:MyEnum){
trace(e.getName());
}