Skip to content

Implement partial function application #188

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

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open

Implement partial function application #188

wants to merge 18 commits into from

Conversation

thekid
Copy link
Member

@thekid thekid commented Jul 12, 2025

This pull request implements limited support for partial function applications (PFA) for PHP 7.4 - 8.4:

In a nutshell

PFA builds on top of callable closure syntax (e.g. $f= strlen(...)) by adding a new placeholder representing a single argument, the question mark (?):

$replace= str_replace('test', 'success', ?); // $replace is a closure
$result= $replace('A test.');                // $result equals "A success."

Implementation

  • Rewrite str_replace('test', 'ok', ?) to fn($arg) => str_replace('test', 'ok', $arg)
  • Make evaluation order consistent with native implementation by evaluating non-constant arguments at declaration
  • Make this work for new expressions (unlike PHP, which does not support this!)
  • Implement optimizations for pipeline operator (emit "test" |> strlen(?) as strlen("test"))

Limitations

  • Support by-ref arguments - this would require runtime reflection checks, resulting in a significant performance hit
  • Invoking with named parameters - same as above, would require runtime reflection
  • Parameter and return type anntotations - same as above, would require runtime reflection
  • Variadic placeholder in the middle of arguments (e.g. $c = stuff(1, ?, ..., p: ?, f: 3.14);)

See also

@thekid
Copy link
Member Author

thekid commented Jul 12, 2025

This PR is now in a pretty complete state (with the limitations listed above), let's wait and see how the vote on the PHP RFC goes, and then feature-test for parity once the RFC's implementation has stabilized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant