Latest Changes

Differences between version EMPTY and #15244

0a1,186
> //Enums// are different to //classes// and are declared with a finite number of //constructors//. Here's a small example:
> 
> <code haxe>
>     enum Color {
>         Red;
>         Green;
>         Blue;
>     }
> 
>     class Colors {
>         static function toInt( c : Color ) : Int {
>             return switch( c ) {
>                 case Red: 0xFF0000;
>                 case Green: 0x00FF00;
>                 case Blue: 0x0000FF;
> 
>             }
>         }
>     }
> </code>
> 
> When you want to ensure that only a fixed number of values are used then //enums// are the best thing since they guarantee that other values cannot be constructed.
> 
> ====== Constructors parameters ======
> 
> The previous ''Color'' sample shows three //constant constructors// for an //enum//. It is also possible to have parameters for constructors :
> 
> <code haxe>
>     enum Color2 {
>         Red;
>         Green;
>         Blue;
>         Grey( v : Int );
>         Rgb( r : Int, g : Int, b : Int );
>     }
> </code>
> 
> This way, there is an infinite number of ''Color2'''s possible, but there are five different constructors possible for it. The following values are all ''Color2'':
> 
> <code haxe>
>     Red;
>     Green;
>     Blue;
>     Grey(0);
>     Grey(128);
>     Rgb( 0x00, 0x12, 0x23 );
>     Rgb( 0xFF, 0xAA, 0xBB );
> </code>
> 
> We can also have a recursive type, for example to add ''alpha'' :
> 
> <code haxe>
>     enum Color3 {
>         Red;
>         Green;
>         Blue;
>         Grey( v : Int );
>         Rgb( r : Int, g : Int, b : Int );
>         Alpha( a : Int, col : Color3 );
>     }
> </code>
> 
> The following are valid ''Color3'' values :
> 
> <code haxe>
>     Alpha( 127, Red );
>     Alpha( 255, Rgb(0,0,0) );
> </code>
> 
> Note that //enums// are immutable, which means the parameters of an Enum are read-only. We cannot and should not change it after initialization.
> 
> ====== Switch on Enum ======
> 
> A switch has a special behavior when used on an //enum//. If there is no ''default'' case then it will check that all an //enum's// constructors are used within the switch, and if not the compiler will generate a warning. For example, consider the first ''Color'' //enum// :
> 
> <code haxe>
>     switch( c ) {
>         case Red: 0xFF0000;
>         case Green: 0x00FF00;
>     }
> </code>
> 
> This will cause a //compile error//warning that the constructor ''blue'' is not used. In this example you can either add a case for it or add a default case that does something. This can be quite useful, as when you add new constructors to your //enum//, compiler errors will alert you to areas in your program where the new constructor should be handled.
> 
> ====== Switch with Constructor Parameters ======
> 
> If //enum// constructor have parameters, they //must// be listed as variable names in a //switch// case. This way all the variables will be locally accessible in the ''case'' expression and correspond to the type of the //enum// constructor parameter. For example, using the ''Color3'' //enum//:
> 
> <code haxe>
>     class Colors {
>         static function toInt( c : Color3 ) : Int {
>             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);
>             }
>         }
> 
>     }
> </code>
> 
> Using ''switch'' is the only possible way to access the //enum// constructors parameters.
> 
> ====== Enum Type Parameters ======
> 
> An //Enum// can also have type parameters. The syntax is the same so here's a small sample of a parameterized linked ''List'' using an //enum// to store the cells :
> 
> <code haxe>
>     enum Cell<T> {
>         Empty;
>         Cons( item : T, next : Cell<T> );
>     }
> 
>     class List<T> {
>         var head : Cell<T>;
> 
>         public function new() {
>             head = Empty;
>         }
> 
>         public function add( item : T ) {
>             head = Cons(item,head);
>         }
> 
>         public function length() : Int {
>             return cellLength(head);
>         }
> 
>         private function cellLength( c : Cell<T> ) : Int {
>             return switch( c ) {
>             case Empty : 0;
>             case Cons(item,next): 1 + cellLength(next);
>             }
>         }
> 
>     }
> </code>
> 
> Using both //enums// and //classes// together can be pretty powerful in some cases.
> 
> ====== Using Enums as default value for parameters ======
> 
> Because an Enum's values are in fact created from a constructor they are not constant and therefore cannot be used as default value for a parameter. However there's a simple work-around :
> 
> <code haxe>
>     enum MyEnum {
>         MyFirstValue;
>         MySecondValue;
>     }
> 
>     class Test {
>         static function withDefaultValuesOnParameters(?a : MyEnum) {
>             if(a == null)
>                 a = MyEnum.MyFirstValue;
>         }
>     }
> </code>
> 
> ====== Enums to and from strings ======
> Use Type.createEnum() method to recreate an enum value from a string:
> <code haxe>
> 	enum EColor {
> 		Red;
> 		Green;
> 		Blue;
> 	}
> 
> 	// To string:
> 	var string = Std.string(EColor.Blue);
> 
> 	// From string:		
> 	var color:EColor = Type.createEnum(EColor, string);
> </code>
> 
> ====== Enum Equality ======
> 
> Use [[/api/Type|Type.enumEq()]] to test for equality between two enums. (Operator == is not guaranteed, empirically will work for enum constructors that take no arguments, but can fail for those taking arguments. All of which can lead you astray: use Type.enumEq() to be correct.)
> 
> ====== Note ======
> Haxe Enums behave very similarly to [[http://en.wikipedia.org/wiki/Tagged_union|tagged unions]], enabling the specification and capture of every "case" of a given method result.  Since all Enum states must be specified in switch statements, this makes them valuable in completely defining the behavior of a given method, as well as ensuring that these behaviors are handled.
> 
> 
> «« [[/ref/type_params]] - [[/ref/packages]] »»
\ No newline at end of file

	
Ver Date Entry Lg User Action
#19502 2013-06-18 02:08:24 doc/flash/security en filt3rek View | Diff
#19501 2013-06-18 02:06:18 doc en filt3rek View | Diff
#19500 2013-06-18 02:00:27 com/libs/feffects en filt3rek View | Diff
#19499 2013-06-17 15:57:18 manual/haxe3/migration ru profelis View | Diff
#19498 2013-06-17 12:15:00 ref/packages ru profelis View | Diff
#19497 2013-06-17 12:14:34 manual/haxe3/migration ru YuriK View | Diff
#19496 2013-06-17 12:05:59 manual/haxe3/migration ru RealyUniqueName View | Diff
#19495 2013-06-17 12:05:13 manual/haxe3/migration ru RealyUniqueName View | Diff
#19494 2013-06-17 12:01:49 manual/haxe3/migration ru profelis View | Diff
#19493 2013-06-17 12:01:49 manual/haxe3/migration ru profelis Set title to Миграция на Haxe 3
#19492 2013-06-17 04:16:12 doc en JLM Restored to version #15937
#19491 2013-06-16 00:26:05 manual/swc en blue112 View | Diff
#19490 2013-06-16 00:25:44 manual/swc en blue112 View | Diff
#19489 2013-06-15 18:06:48 ref/oop jp shohei909 View | Diff
#19488 2013-06-15 17:37:53 ref/properties jp shohei909 View | Diff
#19487 2013-06-15 17:30:50 ref/properties jp shohei909 View | Diff
#19486 2013-06-15 17:30:50 ref/properties jp shohei909 Set title to プロパティ
#19485 2013-06-14 22:20:49 doc en Anonymous View | Diff
#19484 2013-06-13 20:27:48 doc/glossary en JLM View | Diff
#19483 2013-06-13 20:26:44 doc/glossary en JLM View | Diff

Previous | Next