HaxeDoc2

(やまだぃちぅ) #1

3.1.1 Extensions......................................


Extensions are used to express that a structure has all the fields of a given type as well as some
additional fields of its own:
1 typedef IterableWithLength={
2 > Iterable,
3 // read only property
4 var length(default, null):Int;
5 }
6
7 class Extension {
8 static public function main() {
9 var array = [1, 2, 3];
10 var t:IterableWithLength=array;
11 }
12 }


The greater-than operator>denotes that an extension ofIterable<T>is being created, with
the additional class fields following. In this case, a read-only property (4.2)lengthof typeInt
is required.
In order to be compatible withIterableWithLength<T>, a type then must be compatible
withIterable<T>and also provide a read-onlylengthproperty of typeInt. The example
assigns anArray, which happens to fulfill these requirements.
Since Haxe 3.1.0

It is also possible to extend multiple structures:
1 typedef WithLength ={
2 var length(default, null):Int;
3 }
4
5 typedef IterableWithLengthAndPush={
6 > Iterable,
7 > WithLength,
8 function push(a:T):Int;
9 }
10
11 class Extension2 {
12 static public function main() {
13 var array = [1, 2, 3];
14 var t:IterableWithLengthAndPush=
15 array;
16 }
17 }


3.2 Type Parameters.......................................


Haxe allows parametrization of a number of types, as well as class fields ( 4 ) and enum construc-
tors (2.4.1). Type parameters are defined by enclosing comma-separated type parameter names
in angle brackets<>. A simple example from the Haxe Standard Library isArray:
Free download pdf