GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing wiki.php.net — PHP: rfc:first_class_callable_syntax rfc:first_class_callable_syntax
PHP RFC: First-class callable syntax
IntroductionThis RFC proposes the introduction of a first-class callable syntax, which supersedes existing encodings using strings and arrays. The advantage is that the new syntax is accessible to static analysis, and respects the scope at the point where the callable is created. $fn = Closure::fromCallable('strlen'); $fn = strlen(...); $fn = Closure::fromCallable([$this, 'method']); $fn = $this->method(...) $fn = Closure::fromCallable([Foo::class, 'method']); $fn = Foo::method(...);
In this example, each pair of expressions is equivalent. The
The $fn = Foo::method(...); // Think of it as: $fn = fn(...$args) => Foo::method(...$args); The syntax is forward-compatible with partial functions application. Proposal
The syntax
strlen(...); $closure(...); $invokableObject(...); $obj->method(...); $obj->$methodStr(...); ($obj->property)(...); Foo::method(...); $classStr::$methodStr(...); self::{$complex . $expression}(...); 'strlen'(...); [$obj, 'method'](...); [Foo::class, 'method'](...); ScopeAnd advantage of the first-class callable syntax is that it uses the scope at the point where the callable is acquired. Consider the following example: class Test { public function getPrivateMethod() { return [$this, 'privateMethod']; // does not work return Closure::fromCallable([$this, 'privateMethod']); // works, but ugly return $this->privateMethod(...); // works } private function privateMethod() { echo __METHOD__, "\n"; } } $test = new Test; $privateMethod = $test->getPrivateMethod(); $privateMethod();
If a classical array callable like Object creation
The
The general expectation is that fn(...$args) => new Foo(...$args)
and acquire a callable to that trampoline instead. While certainly possible, this takes a step backwards from the straightforward semantics of the Nullsafe callsThe first-class callable syntax cannot be combined with the nullsafe operator. Both of the following result in a compile-time error: $obj?->method(...); $obj?->prop->method(...);
The $fn = $obj?->method(...); // could be $fn = $obj !== null ? $obj->method(...) : null; // or $fn = fn(...$args) => $obj?->method(...$args);
If this syntax were supported, it would likely follow the first interpretation, as the second interpretation does not correspond to a proper callable. However, this behavior is not particularly useful (as the return type is Strict types
The first-class callable syntax interacts with RationalePartial Function ApplicationThis RFC can be seen as an alternative to the partial functions application (PFA) RFC. I believe that PFA use-cases can be divided into roughly three categories: 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 second is use in conjunction with the Pipe Operator (V2) proposal. Taking an example from the RFC: $result = $var |> step_one(?) |> step_two(?, 'config') |> $obj->stepThree('param', ?); However, PFA is only required for this particular form of the pipe operator. The Pipe Operator (V1) proposal instead used a syntax that is specific to the pipe operator: $result = $var |> step_one($$) |> step_two($$, 'config') |> $obj->stepThree('param', $$);
If this definition of the pipe operator is adopted, then PFA is no longer needed for use with the pipe operator. Both approaches to the pipe operator have their advantages. The Finally, while these two are the primary use-cases of PFA, there will also be the occasional usage in other contexts. For example: $array = array_filter($array, str_contains(?, 'foo')); Under this proposal, no dedicated syntax would be available for this use-case, and one would have to use an arrow function: $array = array_filter($array, fn($s) => str_contains($s, 'foo')); 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. The final proposal is simple on a conceptual level, but very involved when it comes to detailed behavior. Syntax choiceThe 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: It represents exactly the callable that would be invoked by a direct call of the same syntax. This cannot be said of some other syntax choices that have been discussed in the past, for example: // Proposed syntax is unambiguous and follows existing semantics: $this->foo(...); // Refers to a method ($this->foo)(...); // Refers to a callable stored in a property // What does this mean? $this->foo::function;
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 "&" sigil: &foo->bar; // Is ambiguous with by-reference assignment: $x = &$foo->bar; // is currently interpreted as $x =& $foo->bar; // Using no sigil: strlen; // Is ambiguous with constant strlen Foo::bar; // Is ambiguous with class constant Foo::bar $foo->bar; // Is ambiguous with object property $foo->bar 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", not a Closure object: strlen::function; // Same as previous, and we'd rather avoid the legacy "callable" terminology. strlen::callable; // Unlike the "&" sigil, this is not ambiguous. It is also not particularly meaningful though. *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 ChangesNone. VoteVoting started on 2021-07-02 and closes on 2021-07-16.
rfc/first_class_callable_syntax.txt · Last modified: by nikic
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |