Prev | Current Page 255 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"

The variable does not need any type declaration because the
complete type can be inferred by the constructor.
The type declared at the moment of the object creation will always remain the same until the object is
garbage collected, and cannot be changed at run time.
Giving the name T to the class parameter is just a convention. More than one class parameter can be
specified using a comma separated list of names between angular brackets.
Type parameters can also be specified at function level; in this case the generic parameter will have a
purpose only in the context of the declaring function and is declared in the same way class type
parameters are but located just after the function name.
125
Chapter 5: Delving Into Object-Oriented Programming
public static function indexOf < T > (v : T, a : Array < T > )
{
for(i in 0...a.length)
if(v == a[i])
return i;
return -1;
}
This function takes as arguments a value of type T and an Array of the same type and returns the
position of the value in the array or ??’ 1 when not found.


Pages:
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267