Prev | Current Page 261 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"

Another difference in the two syntax forms
is that in the first form you can add the private/public access modifier (by default it is always public).
The typedef can also have type parameters as for classes and interfaces.
typedef Leaf < T > = {
parent : Node < T > ,
data : T
}
The typedef syntax is an important feature of the haXe language and one that makes haXe unique.
typedef s are in many ways a better and more flexible alternative to interfaces. The typedef verification
is made only at compile time and it is structural; the compiler checks that the passed object has the
required fields but makes no assumptions on the type definition of the object. That creates very
interesting possibilities. In the following example the function handle() receives as an argument a
value of type HasName . HasName is a typedef that defines that the value of its type must have one field
name of type String . The nice thing is that not only anonymous objects can satisfy this structural
requirement but also class instances.


Pages:
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273