HaxeDoc2

(やまだぃちぅ) #1

mangled names. A specification like this can yield a boost in performance-critical code portions
on static targets (2.2) at the cost of a larger output size:
1 @:generic
2 class MyArray{
3 public function new() { }
4 }
5
6 class Main {
7 static public function main() {
8 var a = new MyArray();
9 var b = new MyArray();
10 }
11 }


It seems unusual to see the explicit typeMyArrayhere as we usually let type
inference (3.6) deal with this. Nonetheless, it is indeed required in this case. The compiler has to
know the exact type of a generic class upon construction. The Javascript output shows the result:
1 (function () { "use strict";
2 var Main = function() { }
3 Main.main = function() {
4 var a = new MyArray_String();
5 var b = new MyArray_Int();
6 }
7 var MyArray_Int = function() {
8 };
9 var MyArray_String = function() {
10 };
11 Main.main();
12 })();


We can identify thatMyArray<String>andMyArray<Int>have becomeMyArray_-
StringandMyArray_Intrespectively. This is similar for generic functions:
1 class Main {
2 static public function main() {
3 method("foo");
4 method(1);
5 }
6
7 @:generic static function method<T>(t:T){}
8 }
Again, the Javascript output makes it obvious:
1 (function () { "use strict";
2 var Main = function() { }
3 Main.method_Int = function(t){
4 }
5 Main.method_String = function(t){
6 }
7 Main.main = function() {
8 Main.method_String("foo");
9 Main.method_Int(1);
Free download pdf