HaxeDoc2

(やまだぃちぅ) #1

Chapter 3


Type System


We learned about the different kinds of types inTypes(Chapter 2) and it is now time to see how
they interact with each other. We start off easy by introducing typedef (3.1), a mechanism to give
a name (or alias) to a more complex type. Among other things, this will come in handy when
working with types having type parameters (3.2).
A lot of type-safety is achieved by checking if two given types of the type groups above
are compatible. Meaning, the compiler tries to performunificationbetween them as detailed in
Unification(Section 3.5).
All types are organized inmodulesand can be addressed throughpaths.Modules and Paths
(Section 3.7) will give a detailed explanation of the related mechanics.

3.1 Typedef............................................


We briefly looked at typedefs while talking about anonymous structures (2.5) and saw how we
could shorten a complex structure type (2.5) by giving it a name. This is precisely what typedefs
are good for. Giving names to structure types might even be considered their primary use. In
fact, it is so common that the distinction appears somewhat blurry and many Haxe users consider
typedefs to actuallybethe structure.
A typedef can give a name to any other type:

1 typedef IA = Array;


This enables us to useIAin places where we would normally useArray<Int>. While this
saves only a few keystrokes in this particular case, it can make a much bigger difference for more
complex, compound types. Again, this is why typedef and structures seem so connected:

1 typedef User ={
2 var age : Int;
3 var name : String;
4 }


A typedef is not a textual replacement but actually a real type. It can even have type parameters
(3.2) as theIterabletype from the Haxe Standard Library demonstrates:

1 typedef Iterable={
2 function iterator() : Iterator;
3 }

Free download pdf