-
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 member method/variables #10
Comments
The parameters of an enum can hold any data, so you need to create variables out of the enum. I think what you are asking can be done, but with a different architecture: using Test.Directions;
class Test {
static function main() trace(Directions.EAST.getOpposite()); // West
}
class Directions {
public static var NORTH:Direction = WithOpposite(South);
public static var SOUTH:Direction = WithOpposite(North);
public static var EAST:Direction = WithOpposite(West);
public static var WEST:Direction = WithOpposite(East);
public static function getOpposite(d:Direction) {
return d.getParameters()[0];
}
}
enum Direction {
WithOpposite(opposite:Direction);
North();
South();
East();
West();
} |
@markknol Thanks for the example. But I understand that these functionalities can be achieved with static extension (actually abstract will do the job even better). My point here is that it would be more convenience to code, because in that case people doesn't need to write the |
Generally, I think what Kevin is after is to be able to just add enum Option<T> {
Some(value:T);
None;
public inline function map<R>(f:T->R)
return switch this {
case Some(v): Some(f(v));
case None: Nonel
}
} Or for the above example: enum Direction {
North;
South;
East;
West;
public var opposite(get, never):Direction;
inline function get_opposite()
return switch this {
case North: South;
case South: North;
case East: West;
case West: East;
}
} |
static vars will be nice too enum Option<T> {
public static var ZERO:Option<Int> = Some(0);
Some(value:T);
None;
} |
Well, yeah, down the line having all the bells and whistles of abstracts on enums would be good. One can do abstracts over enums to have almost the same, except that pattern matching becomes a PITA and generally anything that has to do with constructors just fails. |
And I have attempted to do the exact thing many times. 😄 |
You can add |
You are presuming here that I am importing the whole module that contains the abstract. I could be:
One solution would be to add a little special treatment for abstracts over enums to make them behave exactly the same (IIRC there's already an open issues for that). It would be good enough for me. Then again: enum XImpl<T, U, V> {
/* constructors */
}
abstract X<T, U, V>(XImpl<T, U, V>) from XImpl<T, U, V> to XImpl<T, U, V> {
/* some candy */
}
vs.
enum X<T, U, V> {
/* constructors */
/* some candy */
} I struggle to see the benefit in the verbosity of the former. The the contrary, I would claim it's less clear: practically speaking |
This would indeed be nice, but also require quite a few changes. I'm not sure I'll have the time to look into this... |
Do I need to start a Haxe Project for this? |
Don't think so, this is more about actually implementing it. |
This sounds like a very useful feature. |
Instead of implementing stuff inside Proper enum members could be next step. |
We decided that we don't want this particular feature. However, we'll look into supporting some notion of auto-using which can be applied to type and is going to work pretty much the same way. |
How about the special caring for abstract-over-enum? |
What do you mean exactly? |
These ☝️ |
That's a separate problem, and I think there's still an open issue for this. |
In language like Java you can declare methods/variables for enum right inside the enum itself:
Something like:
This looks handy but I am not quite sure about the difficulty of implementing it and any negative consequences to the language. So I would like to gain some more knowledge about the topic before writing a proposal
The text was updated successfully, but these errors were encountered: