rfc:partial_function_application

This is an old revision of the document!


PHP RFC: Partial Function Application

Introduction

Partial function application is the process of binding only some of the arguments to a function call and leaving the remainder to be bound a later point. In PHP this would be done by a closure.

Proposal

Partial function application is done by using the underscore symbol (_) as a parameter to a function or method call. Instead of calling the function instead a closure will be created and the parameters which are not underscores will be bound. The arity of the closure is the number of underscores used. Underscores can be used in any parameter position. The way the arguments and closed-over variables are bound is determined by the definition of the function that is partially applied; if the function takes that position by reference then the closure will also take that position by reference (or use by reference if applicable). The return type of the closure is also the same as that of the function call it is mirroring.

function f($value, &$ref) {}
 
$array = ['arg' => 0];
 
// the following two statements are equivalent
// (assuming we supported variable assignment for function use-statements)
f(_, $array['arg']);
 
function($value) use($ref = &$array['arg']) {
    return f($value, $ref);
};

Backward Incompatible Changes

A constant of the name “_” will now be a redefinition, which will emit a warning and leave the original value intact. This effectively means you can no longer declare a constant with that name.

PHP Version

This RFC targets 7.2 or 8.0.

Open Issues

Make sure there are no open issues when the vote starts!

Future Scope

This feature

Voting

This RFC requires a 2/3 vote in the affirmative to pass. It will be a simple yes/no vote on whether to include partial function application.

Patches and Tests

Currently no patch.

References

Links to external references, discussions or RFCs

rfc/partial_function_application.1470843779.txt.gz · Last modified: 2017/09/22 13:28 (external edit)