Prev | Current Page 269 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"

round(value * 100) / 100;
}
}
Note that you can use a constructor identifier directly without any prefix; the type name prefix must be
used only in case of ambiguity.
Switch and enum have a great affinity when working together. If you add a gigabyte to
ByteUnit and try to compile again, a compilation error will be raised. This happens
because the switch statement requires that all the possible cases of an enum have to be
contemplated. To bypass the problem it is necessary to provide a new case in the
switch statement or alternatively a fallback default case. This behavior helps detect
errors and inconsistencies at an early stage.
136
Part I: The Core Language
Constructor Arguments
The enum constructors can have arguments just as functions have. To show that feature, a simple query
builder that takes advantage of the strongly typed syntax of haXe is defined as follows:
enum Condition
{
TestIsNull(field : String);
TestNumber(field : String, operator : NumericOp, value : Float);
TestText (field : String, operator : TextOp, value : String);
And (tests : Array < Condition > );
Or (tests : Array < Condition > );
}
enum NumericOp
{
Equal;
Different;
MoreThan;
LessThan;
}
enum TextOp
{
Same;
Like;
}
The enum Condition contains the constructors for each possible test that a query can define (more
definitions are possible, but the list is kept short for sake of clarity).


Pages:
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281