Prev | Current Page 176 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"

For example, instead of performing a
repetitive task of assigning a variable or calling a function in every single case and else if block you
can opt to assign the return value of the if or switch to a single representation of the variable or
function. So, in the case of an if statement, you could do the following:
var myInt = if ( myVal == ???one??? )
1;
else if ( myVal == ???two??? )
2;
else if ...
While a switch statement may look like this:
var myInt = switch ( myVal )
{
case ???one???:
1;
case ???two???:
2;
}
Chapter 4: Controlling the Flow of Information
79
These conditional statements can also form the value passed to a function, so the previous examples
could be output like this:
trace( if ( myVal == ???one??? )
1;
else if ( myVal == ???two??? )
2;
else
-1;
);
And:
trace( switch ( myVal )
{
case ???one???:
1;
case ???two???:
2;
});
Both of these are legal! However, for the sake of readability, it is better to pass the return value to a
temporary variable first, and then supply that variable to the function in question.


Pages:
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188