Explorar o código

generate static extension closures with optional arguments (closes #2497)

Simon Krajewski %!s(int64=10) %!d(string=hai) anos
pai
achega
8a8b5add00

+ 19 - 0
tests/unit/src/unit/issues/Issue2497.hx

@@ -0,0 +1,19 @@
+package unit.issues;
+
+using unit.issues.misc.Issue2497Class;
+
+private class Object1 {
+	public var sayHelloTo: String -> ?String -> String;
+	public function new() {
+		sayHelloTo = "hello".addTail;
+	}
+}
+
+
+
+class Issue2497 extends Test {
+	function test() {
+		var obj1 = new Object1();
+		eq("hello world null", obj1.sayHelloTo("world"));
+	}
+}

+ 8 - 0
tests/unit/src/unit/issues/misc/Issue2497Class.hx

@@ -0,0 +1,8 @@
+package unit.issues.misc;
+
+class Issue2497Class {
+	public static var addTail: String -> String -> ?String-> String = actualFx;
+	public static function actualFx(str1:String, str2:String, ?str3:String) {
+		return str1 + " " + str2 + " " + str3;
+	}
+}

+ 6 - 3
typer.ml

@@ -981,11 +981,14 @@ let rec acc_get ctx g p =
 			let tcallb = TFun (args,ret) in
 			let twrap = TFun ([("_e",false,e.etype)],tcallb) in
 			(* arguments might not have names in case of variable fields of function types, so we generate one (issue #2495) *)
-			let args = List.map (fun (n,_,t) -> if n = "" then gen_local ctx t else alloc_var n t) args in
+			let args = List.map (fun (n,o,t) ->
+				let t = if o then ctx.t.tnull t else t in
+				o,if n = "" then gen_local ctx t else alloc_var n t
+			) args in
 			let ve = alloc_var "_e" e.etype in
-			let ecall = make_call ctx et (List.map (fun v -> mk (TLocal v) v.v_type p) (ve :: args)) ret p in
+			let ecall = make_call ctx et (List.map (fun v -> mk (TLocal v) v.v_type p) (ve :: List.map snd args)) ret p in
 			let ecallb = mk (TFunction {
-				tf_args = List.map (fun v -> v,None) args;
+				tf_args = List.map (fun (o,v) -> v,if o then Some TNull else None) args;
 				tf_type = ret;
 				tf_expr = mk (TReturn (Some ecall)) t_dynamic p;
 			}) tcallb p in