Ver código fonte

allow function types for @:generic type params (fixes #3697)

Aleksandr Kuzmenko 6 anos atrás
pai
commit
72f5e6e290

+ 1 - 0
src/typing/generic.ml

@@ -44,6 +44,7 @@ let make_generic ctx ps pt p =
 					| _ -> (ident_safe (s_type_path_underscore c.cl_path)) ^ (loop_tl tl))
 				| TEnum(en,tl) -> (s_type_path_underscore en.e_path) ^ (loop_tl tl)
 				| TAnon(a) -> "anon_" ^ String.concat "_" (PMap.foldi (fun s f acc -> (s ^ "_" ^ (loop false (follow f.cf_type))) :: acc) a.a_fields [])
+				| TFun(args, return_type) -> "func_" ^ (String.concat "_" (List.map (fun (_, _, t) -> loop false t) args)) ^ "_" ^ (loop false return_type)
 				| TAbstract(a,tl) -> (s_type_path_underscore a.a_path) ^ (loop_tl tl)
 				| _ when not top -> "_" (* allow unknown/incompatible types as type parameters to retain old behavior *)
 				| TMono _ -> raise (Generic_Exception (("Could not determine type for parameter " ^ s), p))

+ 5 - 0
tests/unit/src/unit/TestGeneric.hx

@@ -63,4 +63,9 @@ class TestGeneric extends Test {
 		eq(a.t.a, 1);
 		eq(a.t.b, null);
 	}
+
+	function testGenericFn() {
+		var a = new MyGeneric<Int->Int>(function(i:Int):Int return i * i);
+		eq(4, a.t(2));
+	}
 }

+ 2 - 0
tests/unit/src/unit/TestType.hx

@@ -538,6 +538,7 @@ class TestType extends Test {
 		gf1("foo");
 		gf1(true);
 		gf1({foo: 1});
+		gf1(function(i:Int):String return '$i');
 
 		gf1(new haxe.Template("foo"));
 
@@ -550,6 +551,7 @@ class TestType extends Test {
 
 		hsf(TestType, "gf1_haxe_ds_GenericStack_Int");
 		hsf(TestType, "gf1_anon_foo_Int");
+		hsf(TestType, "gf1_func_Int_String");
 		t(typeError(gf1(null))); // monos don't work
 
 		eq("foo[1,2]", gf2("foo", [1, 2]));