Browse Source

remove Builtin.tuple, add Tuple constructor to Tuple class, add more unit tests for tuples

frabbit 10 years ago
parent
commit
91cae67960
3 changed files with 46 additions and 28 deletions
  1. 2 2
      std/python/lib/Builtin.hx
  2. 17 26
      std/python/lib/Tuple.hx
  3. 27 0
      tests/unit/src/unit/TestPython.hx

+ 2 - 2
std/python/lib/Builtin.hx

@@ -70,10 +70,10 @@ extern class Builtin {
 	//public static function property():Void;
 	//public static function property():Void;
 
 
 
 
-
+	/*
 	@:overload(function <X>():Tuple<X> {})
 	@:overload(function <X>():Tuple<X> {})
 	public static function tuple<X>(a:Array<X>):Tuple<X>;
 	public static function tuple<X>(a:Array<X>):Tuple<X>;
-
+	*/
 
 
 
 
 
 

+ 17 - 26
std/python/lib/Tuple.hx

@@ -4,16 +4,11 @@ package python.lib;
 import python.lib.Builtin;
 import python.lib.Builtin;
 import python.Syntax;
 import python.Syntax;
 
 
-
+@:pythonImport("builtins", "tuple")
 extern class Tuple<X> implements ArrayAccess<X> {
 extern class Tuple<X> implements ArrayAccess<X> {
 
 
-	public static inline function empty<X>():Tuple<X> {
-		return Builtin.tuple();
-	}
-
-	public static inline function fromArray<X>(a:Array<X>):Tuple<X> {
-		return Builtin.tuple(a);
-	}
+	@:overload(function ():Void {})
+	public function new (a:Array<X>):Void;
 
 
 	public var length(get_length, null):Int;
 	public var length(get_length, null):Int;
 
 
@@ -21,10 +16,6 @@ extern class Tuple<X> implements ArrayAccess<X> {
 		return Builtin.len(this);
 		return Builtin.len(this);
 	}
 	}
 
 
-	public inline function at (i:Int):X {
-		return Syntax.arrayAccess(this, i);
-	}
-
 	public inline function toArray ():Array<X>
 	public inline function toArray ():Array<X>
 	{
 	{
 		return Builtin.list(this);
 		return Builtin.list(this);
@@ -36,46 +27,46 @@ extern class Tup2 <A,B> extends Tuple<Dynamic>
 {
 {
 	public static inline function create <A,B>(a:A, b:B):Tup2<A,B> return Syntax.tuple(a,b);
 	public static inline function create <A,B>(a:A, b:B):Tup2<A,B> return Syntax.tuple(a,b);
 	public var _1(get, null):A;
 	public var _1(get, null):A;
-	public inline function get__1():A return Syntax.arrayAccess(this, 0);
+	public inline function get__1():A return this[0];
 	public var _2(get, null):B;
 	public var _2(get, null):B;
-	public inline function get__2():B return Syntax.arrayAccess(this, 1);
+	public inline function get__2():B return this[1];
 }
 }
 
 
 extern class Tup3 <A,B,C> extends Tuple<Dynamic>
 extern class Tup3 <A,B,C> extends Tuple<Dynamic>
 {
 {
 	public static inline function create <A,B,C>(a:A, b:B,c:C):Tup3<A,B,C> return Syntax.tuple(a,b,c);
 	public static inline function create <A,B,C>(a:A, b:B,c:C):Tup3<A,B,C> return Syntax.tuple(a,b,c);
 	public var _1(get, null):A;
 	public var _1(get, null):A;
-	public inline function get__1():A return Syntax.arrayAccess(this, 0);
+	public inline function get__1():A return this[0];
 	public var _2(get, null):B;
 	public var _2(get, null):B;
-	public inline function get__2():B return Syntax.arrayAccess(this, 1);
+	public inline function get__2():B return this[1];
 	public var _3(get, null):C;
 	public var _3(get, null):C;
-	public inline function get__3():C return Syntax.arrayAccess(this, 2);
+	public inline function get__3():C return this[2];
 }
 }
 
 
 extern class Tup4 <A,B,C,D> extends Tuple<Dynamic>
 extern class Tup4 <A,B,C,D> extends Tuple<Dynamic>
 {
 {
 	public static inline function create <A,B,C,D>(a:A, b:B,c:C,d:D):Tup4<A,B,C,D> return Syntax.tuple(a,b,c,d);
 	public static inline function create <A,B,C,D>(a:A, b:B,c:C,d:D):Tup4<A,B,C,D> return Syntax.tuple(a,b,c,d);
 	public var _1(get, null):A;
 	public var _1(get, null):A;
-	public inline function get__1():A return Syntax.arrayAccess(this, 0);
+	public inline function get__1():A return this[0];
 	public var _2(get, null):B;
 	public var _2(get, null):B;
-	public inline function get__2():B return Syntax.arrayAccess(this, 1);
+	public inline function get__2():B return this[1];
 	public var _3(get, null):C;
 	public var _3(get, null):C;
-	public inline function get__3():C return Syntax.arrayAccess(this, 2);
+	public inline function get__3():C return this[2];
 	public var _4(get, null):D;
 	public var _4(get, null):D;
-	public inline function get__4():D return Syntax.arrayAccess(this, 3);
+	public inline function get__4():D return this[3];
 }
 }
 
 
 extern class Tup5 <A,B,C,D,E> extends Tuple<Dynamic>
 extern class Tup5 <A,B,C,D,E> extends Tuple<Dynamic>
 {
 {
 	public static inline function create <A,B,C,D,E>(a:A, b:B,c:C,d:D,e:E):Tup5<A,B,C,D,E> return Syntax.tuple(a,b,c,d,e);
 	public static inline function create <A,B,C,D,E>(a:A, b:B,c:C,d:D,e:E):Tup5<A,B,C,D,E> return Syntax.tuple(a,b,c,d,e);
 	public var _1(get, null):A;
 	public var _1(get, null):A;
-	public inline function get__1():A return Syntax.arrayAccess(this, 0);
+	public inline function get__1():A return this[0];
 	public var _2(get, null):B;
 	public var _2(get, null):B;
-	public inline function get__2():B return Syntax.arrayAccess(this, 1);
+	public inline function get__2():B return this[1];
 	public var _3(get, null):C;
 	public var _3(get, null):C;
-	public inline function get__3():C return Syntax.arrayAccess(this, 2);
+	public inline function get__3():C return this[2];
 	public var _4(get, null):D;
 	public var _4(get, null):D;
-	public inline function get__4():D return Syntax.arrayAccess(this, 3);
+	public inline function get__4():D return this[3];
 	public var _5(get, null):E;
 	public var _5(get, null):E;
-	public inline function get__5():E return Syntax.arrayAccess(this, 4);
+	public inline function get__5():E return this[4];
 }
 }

+ 27 - 0
tests/unit/src/unit/TestPython.hx

@@ -358,6 +358,33 @@ class TestPython extends Test {
 		eq(t._1, 1);
 		eq(t._1, 1);
 		eq(t._2, 2);
 		eq(t._2, 2);
 		eq(t.length, 2);
 		eq(t.length, 2);
+
+		var t = Tup3.create(1, 2, 3);
+		eq(t._1, 1);
+		eq(t._2, 2);
+		eq(t._3, 3);
+		eq(t.length, 3);
+
+		var t = Tup4.create(1, 2, 3, 4);
+		eq(t._1, 1);
+		eq(t._2, 2);
+		eq(t._3, 3);
+		eq(t._4, 4);
+		eq(t.length, 4);
+
+		var t = Tup5.create(1, 2, 3, 4, 5);
+		eq(t._1, 1);
+		eq(t._2, 2);
+		eq(t._3, 3);
+		eq(t._4, 4);
+		eq(t._5, 5);
+		eq(t.length, 5);
+
+		var t = new Tuple([1,2,3]);
+		eq(t[0], 1);
+		eq(t[1], 2);
+		eq(t[2], 3);
+		eq(t.length, 3);
 	}
 	}
 
 
 }
 }