Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have AS3/PHP background and some basic understaning of enums from C++. Saying shortly: I read this:

http://haxe.org/ref/enums

http://haxe.org/doc/cross/more_on_enum

And though - good heavens! What the sorcery is this???

Enum Constructors Parameters? Enum Type Parameters??

Could someone explain with working examples (preferable on http://try.haxe.org/ ) the concept of enums in HaXe for someone coming from languages like PHP and AS3?

What I tried?

Firstly I wrote this:

enum Color3 {
    Red;
    Green;
    Blue;
    Grey( v : Int );
    Rgb( r : Int, g : Int, b : Int );
    Alpha( a : Int, col : Color3 );
}

Then this:

Color3.Grey(5);

And I don't know what to do with this further, and don't know where to search.

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

HaXe enums are algebraic data types (ADTs). Your example in Haskell:

data Color3
  = Red
  | Green
  | Blue
  | Grey { v :: Int }
  | Rgb { r, g, b :: Int }
  | Alpha { a :: Int, col :: Color3 }

In either language, this type simply expresses that a Color3 has a few possible values—red, green, or blue; a grey value; an arbitrary RGB value; or a Color3 value with an additional alpha channel. The fact that some of these constructors take extra parameters should not be alarming. It is simply a terse closed analog to the open behaviour of inheritance:

class Color3 {}

class Red : Color3 {}
class Green : Color3 {}
class Blue : Color3 {}

class Grey : Color3 {
  public var v:int;
  public function Grey(v:int) {
    this.v = v;
  }
}

class Rgb : Color3 {
  public var r:int, g:int, b:int;
  public function Rgb(r:int, g:int, b:int) {
    this.r = r;
    this.g = g;
    this.b = b;
  }
}

class Alpha : Color3 {
  public var a:int;
  public var col:Color3;
  public function Alpha(a:int, col:Color3) {
    this.a = a;
    this.col = col;
  }
}

Here anyone can come along and add a new Color3 child class that may or may not respect the properties we want to enforce. With parameterised enums we have central control over all the constructors, and we also have pattern matching:

return switch (c) {
  case Red: 0xFF0000;
  case Green: 0x00FF00;
  case Blue: 0x0000FF;
  case Grey(v): (v << 16) | (v << 8) | v;
  case Rgb(r,g,b): (r << 16) | (g << 8) | b;
  case Alpha(a,c): (a << 24) | (toInt(c) & 0xFFFFFF);
}

Contrast the alternative of adding a toInt() method to every subclass of Color3. It would be much more code, leading to more potential for errors.

Note that this particular example isn’t great because a few of the constructors are redundant: Rgb subsumes Red, Green, Blue, and Grey, and Alpha can be applied to a colour that already has Alpha, which may not be desirable.

share|improve this answer
    
I know basic concept of enums, but many constructors and parameters - I just don't know how to bite it. In your example is "case Rgb(r,g,b): (r << 16) | (g << 8) | b;" - what are "r", "g", "b" there? Ok, I can track them to declaration "Rgb( r : Int, g : Int, b : Int );", but when and how they got any values? –  Luken Sep 20 '13 at 23:33
    
@Luken: In the case expression, r, g, and b are new local variables which are bound to the values of the corresponding slots by position in the Rgb value. They exist for the duration of the case body. Enum values are much like labelled tuples—for values with no parameters, that tuple is simply empty—and case matching is like the destructuring assignment found in many languages ((x, y) = (1, 2);). –  Jon Purdy Sep 20 '13 at 23:50
    
The problem is that it is hard to grasp without any practical and working example of code. You used analogies with classes, as I can see it - we can able set some values in them, and get those values next. So - how can we set values in enums? And how to get them back (without this special switch mechanic) ? –  Luken Sep 21 '13 at 0:09
    
@Luken: I believe HaXe enums are immutable, though I’m not sure of the specifics. They’re just values, an alternative way to represent data, decoupled from the operations on that data. Classes couple data with behaviour, which is not always what you want. You can make all kinds of things with them: pairs enum Pair<A, B> { Pair(a:A, b:B); }, linked lists enum List<T> { Nil; Cons(head:T, tail:List<T>) }, error handling enum Optional<T> { Failure(reason:String); Success(result:T); }, and more. I think switch is the only way to extract the values. It forces you to handle all cases. –  Jon Purdy Sep 21 '13 at 2:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.