Prev | Current Page 222 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"

Although optional arguments
can be placed anywhere in a function declaration, it is usually a good practice to stack them at the end.
class Main
{
static function main()
{
var sample = new OptionalParameterSample();
trace(sample.quote(???quotation???, ??? - ???));
trace(sample.quote(???quotation???));
}
}
class OptionalParameterSample
{
public function new () { }
public function quote(text : String, ?quotesSymbol : String) : String
{
if(quotesSymbol == null)
quotesSymbol = ????????™;
return quotesSymbol + text + quotesSymbol;
}
}
The second argument of the function quote is optional and when the function is invoked without the
second argument, its value will be null by default. In practice, writing:
sample.quote(???quotation???);
is exactly the same thing as writing:
sample.quote(???quotation???, null);
For this reason, in the body of the quote function, the value of quoteSymbol is tested and, if it is
null , an alternative default value is assigned. Remember that in haXe every variable type, even
numeric types, can assume the value null ; for this reason it ??™ s often prudent to test that a parameter is
actually not a null value or unexpected errors may occur.


Pages:
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234