Skip to content

Commit 4bc33f5

Browse files
committed
Implement support for named arguments
1 parent bfa707c commit 4bc33f5

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

src/main/php/lang/ast/emit/RewritePartialFunctionApplications.class.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,20 @@ protected function emitCallable($result, $callable) {
3434
if ([Placeholder::$VARIADIC] !== $callable->arguments) {
3535
$sig= '';
3636
$pass= $init= [];
37-
foreach ($callable->arguments as $argument) {
37+
foreach ($callable->arguments as $name => $argument) {
3838
if (Placeholder::$VARIADIC === $argument) {
3939
$t= $result->temp();
4040
$sig.= ',...'.$t;
41-
$pass[]= new UnpackExpression(new Variable(substr($t, 1)));
41+
$pass[$name]= new UnpackExpression(new Variable(substr($t, 1)));
4242
} else if (Placeholder::$ARGUMENT === $argument) {
4343
$t= $result->temp();
4444
$sig.= ','.$t;
45-
$pass[]= new Variable(substr($t, 1));
45+
$pass[$name]= new Variable(substr($t, 1));
4646
} else if ($this->isConstant($result, $argument)) {
47-
$pass[]= $argument;
47+
$pass[$name]= $argument;
4848
} else {
4949
$t= $result->temp();
50-
$pass[]= new Variable(substr($t, 1));
50+
$pass[$name]= new Variable(substr($t, 1));
5151
$init[$t]= $argument;
5252
}
5353
}

src/test/php/lang/ast/unittest/emit/CallableSyntaxTest.class.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,4 +335,38 @@ public function run() {
335335
}');
336336
Assert::equals(['hi world'], $r);
337337
}
338+
339+
#[Test]
340+
public function partial_function_application_pass_named() {
341+
$f= $this->run('class %T {
342+
343+
public function run() {
344+
return str_replace(search: "test", replace: "ok", subject: ?);
345+
}
346+
}');
347+
Assert::equals('ok.', $f('test.'));
348+
}
349+
350+
#[Test, Runtime(php: '>=8.0.0')]
351+
public function partial_function_application_named_arguments_out_of_order() {
352+
$f= $this->run('class %T {
353+
354+
public function run() {
355+
return str_replace(subject: ?, replace: "ok", search: "test");
356+
}
357+
}');
358+
Assert::equals('ok.', $f('test.'));
359+
}
360+
361+
#[Test, Runtime(php: '>=8.5.0')]
362+
public function partial_function_application_with_named() {
363+
$r= $this->run('class %T {
364+
365+
public function run() {
366+
$f= str_replace("test", "ok", ?);
367+
return $f(subject: "test.");
368+
}
369+
}');
370+
Assert::equals('ok.', $r);
371+
}
338372
}

0 commit comments

Comments
 (0)