Browse Source

allow @:arrayAccess on abstracts if the underlying type supports it (applies to Vector)

Simon Krajewski 12 years ago
parent
commit
a168fd8861
3 changed files with 15 additions and 2 deletions
  1. 1 0
      std/haxe/Vector.hx
  2. 10 1
      tests/unit/unitstd/haxe/Vector.unit.hx
  3. 4 1
      typer.ml

+ 1 - 0
std/haxe/Vector.hx

@@ -33,6 +33,7 @@ private typedef VectorData<T> = #if flash9
 	A Vector is a storage of fixed size. It can be faster than Array on some
 	targets, and is never slower.
 **/
+@:arrayAccess
 abstract Vector(VectorData<T>)<T> {
 	/**
 		Creates a new Vector of length [length].

+ 10 - 1
tests/unit/unitstd/haxe/Vector.unit.hx

@@ -47,4 +47,13 @@ var data = vec.toData();
 var vec2 = haxe.Vector.fromData(data);
 vec2.get(0) == "1";
 vec2.get(1) == "2";
-vec2.get(2) == "3";
+vec2.get(2) == "3";
+
+// []
+vec2[0] == "1";
+vec2[1] == "2";
+vec2[2] == "3";
+vec2[1] = "4";
+vec2[1] == "4";
+vec2[0] += "a";
+vec2[0] = "1a";

+ 4 - 1
typer.ml

@@ -1854,10 +1854,13 @@ and type_access ctx e p mode =
 				apply_params pl tl (loop (TInst (c,stl)))
 			| TInst ({ cl_path = [],"ArrayAccess" },[t]) ->
 				t
+			| TAbstract(a,tl) when has_meta ":arrayAccess" a.a_meta ->
+				loop (apply_params a.a_types tl a.a_this)
 			| _ ->
 				let pt = mk_mono() in
 				let t = ctx.t.tarray pt in
-				unify ctx e1.etype t e1.epos;
+				(try unify_raise ctx et t p
+				with Error(Unify _,_) -> if not ctx.untyped then error ("Array access is not allowed on " ^ (s_type (print_context()) e1.etype)) e1.epos);
 				pt
 		in
 		let pt = loop e1.etype in