HaxeDoc2

(やまだぃちぅ) #1

1 import haxe.ds.GenericStack;
2
3 class GenericStackExample {
4 static public function main() {
5 var myStack = new GenericStack();
6 for (ii in 0...5)
7 myStack.add(ii);
8 trace(myStack); //{4, 3,2, 1, 0}
9 trace(myStack.pop()); // 4
10 }
11 }


Trivia: FastList
In Haxe 2, the GenericStack class was known as FastList. Since its behavior more closely
resembled a typical stack, the name was changed for Haxe 3.

TheGenericinGenericStackis literal. It is attributed with the:genericmetadata. Depend-
ing on the target, this can lead to improved performance on static targets. SeeGeneric(Sec-
tion 3.3) for more details.

10.2.5 Map


AMapis a container composed ofkey,valuepairs. AMapis also commonly referred to as an as-
sociative array, dictionary, or symbol table. The following code gives a short example of working
with maps:
1 class Main {
2 static public function main() {
3 // Maps are initialized likearrays, but
4 // use ’=>’ operator. Maps canhave their
5 // key value types defined explicity
6 var map1:Map<Int, String>=
7 [1 => "one", 2=>"two"];
8
9 // Or they can infer their keyvalue types
10 var map2 =[
11 "one"=>1,
12 "two"=>2,
13 "three"=>3
14 ];
15 $type(map2); // Map<String, Int>
16
17 // Keys must be unique
18 // Error: Duplicate Key
19 //var map3 = [1=>"dog", 1=>"cat"];
20
21 // Maps values can be accessedusing array
22 // accessors "[]"
23 var map4 =["M"=>"Monday", "T"=>"Tuesday"];
24 trace(map4["M"]); //Monday
25

Free download pdf