Adding a method to an enum.

Charles McAnany mcanance at rose-hulman.edu
Sun Jun 19 19:48:30 PDT 2011


Hi, all. I'm looking for a way to make constants that have methods
without a lot of overhead. In particular, a way to define a
Direction and then be able to rotate it right. Here's kind of what I
have in mind:

enum Direction{
left, right, up, down;

public Direction rotateRight(){
 switch(this){
 case left:
   return up;
 case up:
   return right;
 case right:
   return down;
 case down:
   return left;
 }
}


The best I can think of is to make a new method, which isn't
terrible, but not elegant, either.
Any thoughts?
Thanks,
Charles.


More information about the Digitalmars-d-learn mailing list