-
Notifications
You must be signed in to change notification settings - Fork 59
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
Enum abstract instances #86
base: master
Are you sure you want to change the base?
Conversation
Won't this make our ordinary enums obsolete? |
I guess it's one more step to replace ordinary enums. enum abstract Person( { final name:String; final age:Int; } ) {
final Mark = { name: "Mark", age: 21 };
final John = { name: "John", age: 25 };
final Elisa = { name: "Elisa", age: 28 };
final Fifties(name:String) = { name:name, age:50 }
}
//...
switch person {
case Fifties('Herman'): trace('Herman is 50 years old');
case _:
} |
Some things are a bit confusing for me.
That would mean a Another question (it was tipped in your final questions):
|
We didn't reach a conclusion on this proposal in the haxe-evolution meeting today. There are some open questions here related to the identity and equality of such values. We agreed to postpone the decision here and discuss this at a later point. |
Looking at this proposal, it seems that the core issue here is the inability to associate a single enumerated value with several data points. The syntax provided however seems complicated and confusing for this use case. It may make more sense to extend the functionality of the bare // The syntax for this probably needs a second opinion or refinement
enum Color(Int, Int, Int) {
Rgb(r:Int, g:Int, b:Int);
Red(255, 0, 0);
Green(0, 255, 0);
Blue(0, 0, 255);
Cyan(0, 255, 255);
Magenta(255, 0, 255);
Yellow(255, 255, 0);
DarkRed(r:Int, 0, 0);
}
class Main {
static function main() {
var color = getColor();
switch (color) {
case Red(r, g, b):
trace("Color was red, full intensity " + r);
case DarkRed(r, g, b):
trace("Color was red, intensity " + r);
case Rgb(r, g, b):
trace("Color had a red value of " + r);
}
}
} Although I'm not sure whether this is better than just creating a static extension on the enum and storing the data in a switch/case structure on that. |
This proposal would allow instances as enum abstract values (generated as static singletons).
Currently this is not possible (error:
Inline variable initialization must be a constant value
)Rendered version