Skip to content

Implement first class callable syntax for instance method references #164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php namespace lang\ast\emit;

use lang\ast\nodes\{InstanceExpression, Literal};

/**
* Rewrites `T->func(...)` to `fn(T $t) => $t->func()`.
*
* @see https://externals.io/message/120011
*/
trait CallableInstanceMethodReferences {

protected function emitCallable($result, $callable) {
if (
$callable->expression instanceof InstanceExpression &&
$callable->expression->expression instanceof Literal
) {
$type= $callable->expression->expression->expression;
$result->out->write('static function('.$type.' $self, ... $args) { return $self->');
$this->emitOne($result, $callable->expression->member);
$result->out->write('(...$args); }');
} else {
return parent::emitCallable($result, $callable);
}
}
}

9 changes: 9 additions & 0 deletions src/main/php/lang/ast/emit/CallablesAsClosures.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* @see https://wiki.php.net/rfc/first_class_callable_syntax
*/
trait CallablesAsClosures {
use CallableInstanceMethodReferences { emitCallable as callableInstanceMethodReferences; }

private function emitQuoted($result, $node) {
if ($node instanceof Literal) {
Expand Down Expand Up @@ -48,6 +49,14 @@ private function emitQuoted($result, $node) {
}

protected function emitCallable($result, $callable) {
if (
$callable->expression instanceof InstanceExpression &&
$callable->expression->expression instanceof Literal
) {
$this->callableInstanceMethodReferences($result, $callable);
return;
}

$result->out->write('\Closure::fromCallable(');
$this->emitQuoted($result, $callable->expression);
$result->out->write(')');
Expand Down
9 changes: 9 additions & 0 deletions src/main/php/lang/ast/emit/PHP70.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* @see https://wiki.php.net/rfc#php_70
*/
class PHP70 extends PHP {
use CallableInstanceMethodReferences { emitCallable as callableInstanceMethodReferences; }
use
ArbitrayNewExpressions,
ArrayUnpackUsingMerge,
Expand Down Expand Up @@ -61,6 +62,14 @@ public function __construct() {
}

protected function emitCallable($result, $callable) {
if (
$callable->expression instanceof InstanceExpression &&
$callable->expression->expression instanceof Literal
) {
$this->callableInstanceMethodReferences($result, $callable);
return;
}

$t= $result->temp();
$result->out->write('(is_callable('.$t.'=');
if ($callable->expression instanceof Literal) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/php/lang/ast/emit/PHP81.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* @see https://wiki.php.net/rfc#php_81
*/
class PHP81 extends PHP {
use RewriteBlockLambdaExpressions, RewriteDynamicClassConstants, ReadonlyClasses;
use RewriteBlockLambdaExpressions, RewriteDynamicClassConstants, ReadonlyClasses, CallableInstanceMethodReferences;

/** Sets up type => literal mappings */
public function __construct() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/php/lang/ast/emit/PHP82.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* @see https://wiki.php.net/rfc#php_82
*/
class PHP82 extends PHP {
use RewriteBlockLambdaExpressions, RewriteDynamicClassConstants, ReadonlyClasses;
use RewriteBlockLambdaExpressions, RewriteDynamicClassConstants, ReadonlyClasses, CallableInstanceMethodReferences;

/** Sets up type => literal mappings */
public function __construct() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/php/lang/ast/emit/PHP83.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @see https://wiki.php.net/rfc#php_83
*/
class PHP83 extends PHP {
use RewriteBlockLambdaExpressions, ReadonlyClasses;
use RewriteBlockLambdaExpressions, ReadonlyClasses, CallableInstanceMethodReferences;

/** Sets up type => literal mappings */
public function __construct() {
Expand Down
23 changes: 23 additions & 0 deletions src/test/php/lang/ast/unittest/emit/CallableSyntaxTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,27 @@ public function __construct($value) { $this->value= $value; }
}');
Assert::equals($this, $f($this)->value);
}

#[Test]
public function instance_method_reference_map() {
$r= $this->run('use lang\ast\unittest\emit\Handle; class <T> {
public function run() {
$handles= [new Handle(0), new Handle(1), new Handle(2)];
return array_map(Handle->hashCode(...), $handles);
}
}');
Assert::equals(['#0', '#1', '#2'], $r);
}

#[Test]
public function instance_method_reference_sort() {
$r= $this->run('use util\Date; class <T> {
public function run() {
$dates= [new Date(43200), new Date(86400), new Date(0)];
usort($dates, Date->compareTo(...));
return array_map(Date->getTime(...), $dates);
}
}');
Assert::equals([86400, 43200, 0], $r);
}
}