[RFC] Pre-draft for PipeOp v2

From: Date: Tue, 19 Sep 2017 22:32:24 +0000
Subject: [RFC] Pre-draft for PipeOp v2
Groups: php.internals 
Request: Send a blank email to internals+get-100706@lists.php.net to get a copy of this message
I was planning to update the RFC, but wiki.php.net is having issues atm and isn't coming back up with basic coaxing, so I'll just start discussion informally, and the RFC can be updates later. Background: I made an RFC some time ago to implement HackLang's Pipe Operator https://docs.hhvm.com/hack/operators/pipe-operator which provides fluent calling for non-object interfaces. I circulated it to mixed reviews, many negative, with the negativity feeling like it centered on the use of a magic placeholder token $$. After discussion with Levi and others who suggested a simpler approach, I'd like to offer https://github.com/php/php-src/compare/master...sgolemon:pipe2 as an alternate possibility. This version removes the $$ token, and instead treats the RHS of the expression as a callable obeying the same rules as the callable typehint elsewhere in the language. Specifically: * Free functions as strings containing the function name. (e.g. 'funcname') * Object methods as array($object, 'methodname') * Static methods as array('Classname', 'methodname') * Closure expression (e.g. function($x) { return ...; } ) * Object instance with an __invoke() method. In a given pipe expression, the output of the LHS expression feeds a single arg to the callable on the RHS. Examples: $x = "hello" |> 'strtoupper' |> function($x) { return $x . " world"; }; // $x === "HELLO world" Non-Goal: I didn't include support for base function names (e.g. "hello" |> strtoupper) because of the conflict with constants. Using a constant to store your function name is totes legit and consistent with language syntax. Future Scope: Short Lambdas $x => $x + 1 and Partial Functions someFunc('fixed val1', ..., 'fixed val2') would help make this functionality more useful and are worth discussing as a sub-thread, but are not required to be implemented at the same time. -Sara

« previous php.internals (#100706) next »