rfc:first_class_callable_syntax
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
rfc:first_class_callable_syntax [2021/05/20 11:16] – nikic | rfc:first_class_callable_syntax [2021/07/02 10:42] – nikic | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: First-class callable syntax ====== | ====== PHP RFC: First-class callable syntax ====== | ||
* Date: 2021-05-20 | * Date: 2021-05-20 | ||
- | * Author: Nikita Popov < | + | * Author: Nikita Popov <nikic@php.net>, |
- | * Status: | + | * Status: |
* Target Version: PHP 8.1 | * Target Version: PHP 8.1 | ||
* Implementation: | * Implementation: | ||
Line 22: | Line 22: | ||
In this example, each pair of expressions is equivalent. The '' | In this example, each pair of expressions is equivalent. The '' | ||
+ | |||
+ | The '' | ||
+ | |||
+ | <PHP> | ||
+ | $fn = Foo:: | ||
+ | // Think of it as: | ||
+ | $fn = fn(...$args) => Foo:: | ||
+ | </ | ||
+ | |||
+ | The syntax is forward-compatible with [[rfc: | ||
===== Proposal ===== | ===== Proposal ===== | ||
Line 79: | Line 89: | ||
and acquire a callable to that trampoline instead. While certainly possible, this takes a step backwards from the straightforward semantics of the '' | and acquire a callable to that trampoline instead. While certainly possible, this takes a step backwards from the straightforward semantics of the '' | ||
+ | |||
+ | ==== Nullsafe calls ==== | ||
+ | |||
+ | The first-class callable syntax cannot be combined with the nullsafe operator. Both of the following result in a compile-time error: | ||
+ | |||
+ | <PHP> | ||
+ | $obj? | ||
+ | $obj? | ||
+ | </ | ||
+ | |||
+ | The '' | ||
+ | |||
+ | <PHP> | ||
+ | $fn = $obj? | ||
+ | // could be | ||
+ | $fn = $obj !== null ? $obj-> | ||
+ | // or | ||
+ | $fn = fn(...$args) => $obj? | ||
+ | </ | ||
+ | |||
+ | If this syntax were supported, it would likely follow the first interpretation, | ||
+ | |||
+ | ==== Strict types ==== | ||
+ | |||
+ | The first-class callable syntax interacts with '' | ||
===== Rationale ===== | ===== Rationale ===== | ||
Line 84: | Line 119: | ||
==== Partial Function Application ==== | ==== Partial Function Application ==== | ||
- | This RFC is intended | + | This RFC can be seen as an alternative to the [[rfc: |
The first is the use of PFA to acquire a callable, without partially applying any arguments. I believe that the vast majority of PFA uses would be for this purposes. This RFC proposes to provide special support for this use-case **only**. | The first is the use of PFA to acquire a callable, without partially applying any arguments. I believe that the vast majority of PFA uses would be for this purposes. This RFC proposes to provide special support for this use-case **only**. | ||
Line 122: | Line 157: | ||
I think that the existing syntax is already sufficiently concise that there is no strong need to introduce an even shorter one. | I think that the existing syntax is already sufficiently concise that there is no strong need to introduce an even shorter one. | ||
- | As such, I believe that adding a first-class callable syntax, and using the original approach to the pipe operator, would give us most of the benefit of PFA at a much lower complexity cost. The PFA proposal has gone through many iterations, because nailing down the precise semantics turned out to be surprisingly hard. According to Joe Watkins (the implementer of the PFA RFC), the final semantics we have arrived at (which, at the time of this writing, are not reflected in the PFA proposal | + | As such, I believe that adding a first-class callable syntax, and using the original approach to the pipe operator, would give us most of the benefit of PFA at a much lower complexity cost. The PFA proposal has gone through many iterations, because nailing down the precise semantics turned out to be surprisingly hard. The final proposal |
==== Syntax choice ==== | ==== Syntax choice ==== | ||
- | The proposed syntax is forward-compatible with the latest iteration of the PFA proposal | + | The proposed syntax is forward-compatible with the latest iteration of the PFA proposal. As such, it would be possible to expand it into a full PFA feature in the future. |
The call-based syntax also has the advantage that it is unambiguous: | The call-based syntax also has the advantage that it is unambiguous: | ||
Line 136: | Line 171: | ||
// What does this mean? | // What does this mean? | ||
- | $this-> | + | $this-> |
</ | </ | ||
- | I am generally open to using a different syntax | + | This can be resolved by limiting the '':: |
+ | |||
+ | A problem with the '' | ||
+ | |||
+ | Here are some commonly suggested syntax choices for first-class callables that are definitely **not** possible due to ambiguities: | ||
+ | |||
+ | < | ||
+ | // Using "&" | ||
+ | & | ||
+ | // Is ambiguous with by-reference assignment: | ||
+ | |||
+ | $x = & | ||
+ | // is currently interpreted as | ||
+ | $x =& $foo-> | ||
+ | |||
+ | // Using no sigil: | ||
+ | strlen; // Is ambiguous with constant strlen | ||
+ | Foo::bar; // Is ambiguous with class constant Foo::bar | ||
+ | $foo-> | ||
+ | </ | ||
+ | |||
+ | Here are syntax choices that are (mostly) unambiguous if only usage with proper symbols is allowed: | ||
+ | |||
+ | < | ||
+ | // As mentioned above, people might expect this to return " | ||
+ | strlen:: | ||
+ | // Same as previous, and we'd rather avoid the legacy " | ||
+ | strlen:: | ||
+ | |||
+ | // Unlike the "&" | ||
+ | *strlen; | ||
+ | // This also applies to various other sigils that are not yet used in unary position, but are equally meaningless: | ||
+ | ^strlen; | ||
+ | </ | ||
+ | |||
+ | I am generally open to using a different syntax, as I don't think forward-compatibility with a potential PFA feature is critical, but none of the choices are particularly great. | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
Line 147: | Line 217: | ||
===== Vote ===== | ===== Vote ===== | ||
- | Yes/No. | + | Voting started on 2021-07-02 and closes on 2021-07-16. |
+ | |||
+ | <doodle title=" | ||
+ | | ||
+ | | ||
+ | </ |
rfc/first_class_callable_syntax.txt · Last modified: 2021/07/16 09:55 by nikic