www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Adding a method to an enum.

reply Charles McAnany <mcanance rose-hulman.edu> writes:
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.
Jun 19 2011
next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On 2011-06-19 19:48, Charles McAnany wrote:
 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?
Well, you can have a function like that no problem. But you can't add it to an enum. It's just a free-standing function which takes an enum and returns the Direction to its right. Now, in theory, you could define an enum which was a struct which had a function which returned the Direction to the right, but you can't currently have enums of struct type with more than one value ( http://d.puremagic.com/issues/show_bug.cgi?id=4423 ), though it's supposed to. Of course, defining such a function could be entertaining given that it would effectively be recursive in nature as far as its definition goes (since each enum would be of the struct type and wouldn't exist unless the struct type can be instantiated, but the struct type couldn't have such a function unless it could be instantiated...). So, I don't know if you could get it to work with a struct anyway. The simplest thing to do is to simply have a free function which takes a Direction and returns the Direction to its right. There shouldn't be any need to be able to call the function on the enum itself. - Jonathan M Davis
Jun 19 2011
prev sibling parent Ali =?iso-8859-1?q?=C7ehreli?= <acehreli yahoo.com> writes:
On Mon, 20 Jun 2011 02:48:30 +0000, Charles McAnany wrote:

 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;
  }
 }
It may be acceptable to change a Direction variable freely in a different context. So I think that the requirement on how a Direction variable change should not be on the Direction type itself. How about a Dial that has a Direction: struct Dial { Direction direction_; void rotateRight(int count = 1) { direction_ += count; direction_ %= Direction.sizeof; } void rotateLeft(int count = 1) { rotateRight(-count); } property Direction direction() const { return direction_; } } Ali
Jun 19 2011