-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathTuple.hx
85 lines (76 loc) · 2.21 KB
/
Tuple.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package moon.core;
/**
* A tuple is fixed-length, and every entry is typed.
* Tuple is a TupleData abstract, which is an Array<Dynamic> abstract.
*
* Usage:
* var a = new Tuple<Int, String, Float>(5, "foo", 1.23);
* trace(a.v0);
* trace(a.v1);
* trace(a.v2);
*
* Shorter way of creating a tuple:
* var a = Tuple.of(5, "foo", 1.23);
*
* Compile-time type checking:
* a.v1 = 1; // Error, expected String, Int given.
*
* Note: Should this be a Vector<Dynamic> abstract instead?
*
* @author Munir Hussin
*/
#if !macro
@:genericBuild(moon.macros.tuple.TupleMacro.build())
class Tuple<Rest>
{
//@:noCompletion
private var data(default, null):Array<Dynamic>;
public var length(get, never):Int;
private inline function get_length():Int
{
return data.length;
}
public inline function iterator():Iterator<Dynamic>
{
return data.iterator();
}
/**
* Returns an array representation of this tuple.
* Internally, a tuple is an Array<Dynamic>. Calling
* this method will return a copy of that array.
*/
public inline function toArray():Array<Dynamic>
{
return data.copy();
}
public inline function toString():String
{
return "(" + data.join(", ") + ")";
}
/**
* Macro to create a typed tuple of any arity.
*
* var a = Tuple.of(1, "hello");
* is equivalent to
* var a = new Tuple<Int, String>(1, "hello");
*
* This works in 3.2 but no longer works in 3.3.
* In 3.3, Tuple.of(1, "hello"), Tuple is resolved to Tuple0,
* and the compiler gives the error: Type not found: moon.core.Tuple0
*/
public static macro function of(args:Array<haxe.macro.Expr>):haxe.macro.Expr
{
var typeParams:Array<haxe.macro.Expr.TypeParam> = [];
for (r in args)
{
var ct = haxe.macro.TypeTools.toComplexType(haxe.macro.Context.typeof(r));
typeParams.push(haxe.macro.Expr.TypeParam.TPType(ct));
}
return
{
expr: ENew({ pack: ["moon", "core"], name: "Tuple", params: typeParams }, args),
pos: haxe.macro.Context.currentPos()
};
}
}
#end