Skip to content

POC https://wiki.php.net/rfc/function-composition #18800

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
18 changes: 18 additions & 0 deletions Zend/tests/composed_callable_001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Composed callable using direct object methods.
--FILE--
<?php

$cb = new \ComposedCallable(strtolower(...));
var_dump($cb("Hello"));
$cb->prepend(fn($x) => "$x World");
var_dump($cb("Hello"));
$cb->append('strrev');
var_dump($cb("Hello"));
$cb->insert(ucfirst(...), 2);
var_dump($cb("Hello"));
--EXPECT--
string(5) "hello"
string(11) "hello world"
string(11) "dlrow olleh"
string(11) "dlrow olleH"
12 changes: 12 additions & 0 deletions Zend/tests/composed_callable_002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Composed callable using composition.
--FILE--
<?php

$cb1 = new \ComposedCallable([strtolower(...)]);
$cb2 = $cb1 + strrev(...);
var_dump($cb1("Hello World"));
var_dump($cb2("Hello World"));
--EXPECT--
string(11) "hello world"
string(11) "dlrow olleh"
9 changes: 9 additions & 0 deletions Zend/tests/composed_callable_003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
--TEST--
Composed callable using composition and auto-promotion
--FILE--
<?php

$cb = strtolower(...) + str_rot13(...) + (fn($x) => "<$x>");
var_dump($cb("Hello World"));
--EXPECT--
string(13) "<uryyb jbeyq>"
10 changes: 10 additions & 0 deletions Zend/zend_closures.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "zend.h"
#include "zend_API.h"
#include "zend_closures.h"
#include "zend_composed_callable.h"
#include "zend_exceptions.h"
#include "zend_interfaces.h"
#include "zend_objects.h"
Expand Down Expand Up @@ -705,6 +706,14 @@ ZEND_COLD ZEND_METHOD(Closure, __construct)
}
/* }}} */

static zend_result zend_closure_do_operation(uint8_t opcode, zval *result, zval *op1, zval *op2) {
if (opcode != ZEND_ADD) {
return FAILURE;
}

return zend_composed_callable_new_from_pair(result, op1, op2);
}

void zend_register_closure_ce(void) /* {{{ */
{
zend_ce_closure = register_class_Closure();
Expand All @@ -720,6 +729,7 @@ void zend_register_closure_ce(void) /* {{{ */
closure_handlers.get_debug_info = zend_closure_get_debug_info;
closure_handlers.get_closure = zend_closure_get_closure;
closure_handlers.get_gc = zend_closure_get_gc;
closure_handlers.do_operation = zend_closure_do_operation;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting! That's not at all how I would have thought to do it.

If I follow, this is implemented as an operator override on closures, rather than special casing the operator itself, yes?

}
/* }}} */

Expand Down
Loading
Loading