rfc:pipe-operator

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
rfc:pipe-operator [2016/05/04 03:11]
pollita Typo: $app -> $$
rfc:pipe-operator [2017/09/22 13:28] (current)
Line 1: Line 1:
 ====== PHP RFC: Pipe Operator ====== ====== PHP RFC: Pipe Operator ======
-  * Version: 0.1+  * Version: 0.3
   * Date: 2016-04-29   * Date: 2016-04-29
-  * Author: Sara Golemon <pollita@php.net>+  * Author: Sara Golemon <pollita@php.net>, Marcelo Camargo <marcelocamargo@linuxmail.org>
   * Status: Under Discussion   * Status: Under Discussion
   * First Published at: http://wiki.php.net/rfc/pipe-operator   * First Published at: http://wiki.php.net/rfc/pipe-operator
Line 8: Line 8:
 ===== Introduction ===== ===== Introduction =====
  
-Complex nested expressions can become progressively difficult to read the deeper they go with function returns leading into arguments to functions which return an argument to another call and so on and so forth.  This RFC aims to improve code readability by decomposing complex nested expressions into series of linearly readable chained statements.+A common PHP OOP pattern is the use of method chaining, or what is also known as "Fluent Expressions". So named for the way one method flows into the next to form conceptual hyper-expression.
  
-===== Proposal =====+For example, the following shows a SQL query expression built out of component pieces, then executed:
  
-Introduce the "Pipe Operator" `|>`, a binary expression which uses the result of the LHS as an input to the RHS expression at an arbitrary point denoted by the additional "Pipe Replacement Variable" expression `$$`.  The result of the RHS expressionafter substitutionis used as the result of the operator.+<code php> 
 +$rs = $db 
 +  ->select()->fields('id''name''email'
 +  ->from('user_table'
 +  ->whereLike(['email' => '%@example.com']) 
 +  ->orderAsc('id'
 +  ->execute(); 
 +</code>
  
-This feature is being culled from HackLang 3.13 and the manual page for it may be referenced at: https://docs.hhvm.com/hack/operators/pipe-operator+This works well enough for OOP classes which were designed for fluent calling, however it is impossible, or at least unnecessarily arduous, to adapt non-fluent classes to this usage style, harder still for functional interfaces.
  
-==== PSR7 Example ====+While decomposing these expressions to make use of multiple variables is an option, this can lead to reduced readability, polluted symbol tables, or static-analysis defying type inconsistency such as in the following two examples:
  
-Here's the pseudo-code for a typical request/response dispatch cycle:+<code php> 
 +$config = loadConfig(); 
 +$dic = buildDic($config); 
 +$app = getApp($dic); 
 +$router = getRouter($app); 
 +$dispatcher = getDispatcher($router, $request); 
 +$logic = dispatchBusinessLogic($dispatcher, $request, new Response()); 
 +$render = renderResponse($logic); 
 +$psr7 = buildPsr7Response($render); 
 +$response = emit($psr7); 
 +</code>
  
-    $request = getGlobals() +Or:
-     |> parseRequest($$) +
-     |> buildPsr7Request($$);+
  
-    $response = loadConfig() +<code php> 
-     |> buildDic($$+$= loadConfig(); 
-     |> getApp($$+$x = buildDic($x); 
-     |> getRouter($$+$x = getApp($x); 
-     |> getDispatcher($$, $request) +$x = getRouter($x); 
-     |> dispatchBusinessLogic($$, $request, new Response()) +$x = getDispatcher($x, $request); 
-     |> renderResponse($$+$x = dispatchBusinessLogic($x, $request, new Response()); 
-     |> buildPsr7Response($$+$x = renderResponse($x); 
-     |> emit($$);+$x = buildPsr7Response($x); 
 +$response = emit($x); 
 +</code>
  
-Here's what it would look like with a separate approach (more business-oriented):+This may lead to error prone code, enforcing reassigment or pollution of the scope for readability. These sorts of chains could also, conceivably, be handled using deeply nested expressions, but the code becomes even more unreadable this way:
  
-    buildRequest() // (basically the first part of the previous example here) +<code php> 
-     |> validate($$) +$dispatcher = getDispatcher(getRouter(getApp(buildDic(loadConfig()))), $request); 
-     |> convertToCommand($$) +$response = emit(buildPsr7Response(renderResponse(dispatchBusinessLogic($dispatcher, $request, new Response())))); 
-     |> execute($$) +</code>
-     |> convertToViewModel($$) +
-     |> render($$) +
-     |> convertToHttpResponse($$) +
-     |> emit($$);+
  
-==== File collection Example ====+This RFC aims to improve code readability by bringing fluent expressions to functional and OOP libraries not originally designed for the task. Several languages already provide support for the pipe operator or, at least, for definining it. Some of the languages that offer support are Elixir, F#, LiveScript and HackLang.
  
-As an example, consider the following real block of code I wrote while creating a test importer (to migrate HHVM format tests into PHPT format):+===== Proposal ===== 
 + 
 +Introduce the "Pipe Operator" ''|>'', mirroring the method call operator ''->''. This expression acts as a binary operation using the result of the LHS as an input to the RHS expression at an arbitrary point denoted by the additional "Pipe Replacement Variable" expression ''$$''. The result of the RHS expression, after substitution, is used as the result of the operator. 
 + 
 +This feature has already been implemented in HackLang, and the manual page for it may be referenced at: https://docs.hhvm.com/hack/operators/pipe-operator 
 + 
 +==== PSR7 Example ==== 
 + 
 +Here's the equivalent chain of function calls as demonstrated in the intro section above: 
 + 
 +<code php> 
 +$response = loadConfig() 
 +  |> buildDic($$) 
 +  |> getApp($$) 
 +  |> getRouter($$) 
 +  |> getDispatcher($$, $request) 
 +  |> dispatchBusinessLogic($$, $request, new Response()) 
 +  |> renderResponse($$) 
 +  |> buildPsr7Response($$) 
 +  |> emit($$); 
 +</code> 
 + 
 +==== File Collection Example ==== 
 + 
 +As an example, consider the following real block of code I wrote while creating a test importer (to migrate HHVM format tests into PHPT format). Please try not to get hung up into whether or not it's "good" PHP code, but rather that it's solving a problem, which is precisely what PHP is designed to do:
  
-  $ret =  +<code php> 
-    array_merge( +$ret = 
-      $ret, +  array_merge( 
-      getFileArg( +    $ret, 
-        array_map( +    getFileArg( 
-          function ($x) use ($arg) { return $arg . '/' . $x; }, +      array_map( 
-          array_filter( +        function ($x) use ($arg) { return $arg . '/' . $x; }, 
-            scandir($arg), +        array_filter( 
-            function ($x) { return $x !== '.' && $x !== '..'); } +          scandir($arg), 
-          )+          function ($x) { return $x !== '.' && $x !== '..'); }
         )         )
       )       )
-    ); +    
 +  ); 
 +</code>
  
 This block of code is readable, but one must carefully examine the nesting to determine what the initial input it, and what order it traverses the steps involved. This block of code is readable, but one must carefully examine the nesting to determine what the initial input it, and what order it traverses the steps involved.
  
-With this proposal, the above could be rewritten as:+With this proposal, the above could be easily rewritten as:
  
-  $ret = scandir($arg) +<code php> 
-      |> array_filter($$, function($x) { return $x !== '.' && $x != '..'; }) +$ret = scandir($arg) 
-      |> array_map(function ($x) use ($arg) { return $arg . '/' . $x; }, $$) +  |> array_filter($$, function($x) { return $x !== '.' && $x != '..'; }) 
-      |> getFileArg($$) +  |> array_map(function ($x) use ($arg) { return $arg . '/' . $x; }, $$) 
-      |> array_merge($ret, $$);+  |> getFileArg($$) 
 +  |> array_merge($ret, $$); 
 +</code>
  
-This clearly, and unambiguously shows `scandir()` as the initial source of data, that it goes through an `array_filter` to avoid recursion, an `array_map` to requalify the paths, some local function, and finally a merge to combine the result with a collector variable.+This, cleary and unambiguouslyshows `scandir()` as the initial source of data, that it goes through an `array_filter` to avoid recursion, an `array_map` to requalify the paths, some local function, and finally a merge to combine the result with a collector variable
 + 
 +==== FBShipIt Example ==== 
 + 
 +Also consider [[https://github.com/facebook/fbshipit/blob/a995e82/fb-examples/lib/FBCommonFilters.php-example#L20,L41|the follow segment of code]] which is used in production by FBShipIt to translate and export nearly all of Facebook's Opensource projects to github: 
 + 
 +<code php> 
 +return $changeset 
 +  |> self::skipIfAlreadyOnGitHub($$) 
 +  |> self::stripCommonFiles( 
 +      $$, 
 +      $config['stripCommonFiles/exceptions'] ?? ImmVector {}, 
 +    ) 
 +  |> self::stripSubjectTags($$) 
 +  |> self::stripCommands($$) 
 +  |> self::delinkifyDifferentialURLs($$) 
 +  |> self::restoreGitHubAuthor($$) 
 +  |> ShipItUserFilters::rewriteSVNAuthor( 
 +      $$, 
 +      FBToGitHubUserInfo::class, 
 +    ) 
 +  |> self::filterMessageSections( 
 +      $$, 
 +      $config['filterMessageSections/keepFields'
 +        ?? self::getDefaultMessageSectionNames(), 
 +    ) 
 +  |> self::rewriteMentions($$) 
 +  |> self::rewriteReviewers($$) 
 +  |> self::rewriteAuthor($$); 
 +</code> 
 + 
 +This presents every step taken by the common filter chain in an easy to follow list of actions.
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
Line 80: Line 151:
 While most ambiguities of `$$` between pipe replacement variable and variable variables are covered in the lexer rule, the following case is not accounted for: While most ambiguities of `$$` between pipe replacement variable and variable variables are covered in the lexer rule, the following case is not accounted for:
  
-  $a = 1; +<code php> 
-  $b = 'a'; +$a = 1; 
-  var_dump($$ /* comment */ {'b'}); +$b = 'a'; 
-  // Expected: int(1) +var_dump($$ /* comment */ {'b'}); 
-  // Actual: Use of $$ outside of a pipe expression+// Expected: int(1) 
 +// Actual: Use of $$ outside of a pipe expression 
 +</code>
  
-This particular quirk of the parser (allowing comments in the middle of a variable-variable-brace-expression) is doubtlessly a rare occurance in the wild, so the current implementation stopped short of trying to resolve it.+This particular quirk of the parser (allowing comments in the middle of a variable-variable-brace-expression) is doubtlessly a rare occurrence in the wild, so the current implementation stopped short of trying to resolve it.
  
 Potential resolutions: Potential resolutions:
-  * Use a less-ambiguous token.  `$>` which mirrors `|>` is my personal favorite.  Downshot: doesn't match HackLang +  * Use a less-ambiguous token. `$>`which mirrors `|>`is my personal favorite. Downshot: doesn't match HackLang 
-  * Get very creative in the parser.  Since '$' '{' expr '}' is handled by the parser, then perhaps '$' '$' should as well.  So far, attempts to resolve this result in conflicts.  More work may yet yield results.+  * Get very creative in the parser. Since '$' '{' expr '}' is handled by the parser, then perhaps '$' '$' should as well. So far, attempts to resolve this result in conflicts. More work may yet yield results.
  
-Note that HHVM does not handle this case either.  Nor, in fact, does it handle mere whitespace between `$$` and `{expr}`, which the attached PHP implementation does.+Note that HHVM does not handle this case either. Nor, in fact, does it handle mere whitespace between `$$` and `{expr}`, which the attached PHP implementation does.
  
-**Update:** HackLang is normally supposed to disallow variable-variables, so the use of `$$` was seen as non-conflicting.  A bug in the 3.13 implementation of pipe operator meant that variable-variables temporarily wound up working where they should not have.  So whatever we propose for Pipe Operator's substitution will face the same issues in both lexers eventually anyhow.+**Update:** HackLang is normally supposed to disallow variable-variables, so the use of `$$` was seen as non-conflicting. A bug in the 3.13 implementation of pipe operator meant that variable-variables temporarily wound up working where they should not have. So whatever we propose for Pipe Operator's substitution will face the same issues in both lexers eventually anyhow.
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
-7.Next+7.2
  
 ===== Open Issues ===== ===== Open Issues =====
Line 103: Line 176:
  
 ===== Future Scope ===== ===== Future Scope =====
-The current proposal limits use of the `$$` to a single replacement per expression.  This feature could potentially be expanded to allow multiple uses of `$$` within a single RHS expression.+The current proposal limits use of the `$$` to a single replacement per expression. This feature could potentially be expanded to allow multiple uses of `$$` within a single RHS expression. 
 + 
 +===== Third-party Arguments ===== 
 + 
 +Informal Twitter poll (821 respondents) results: https://twitter.com/SaraMG/status/727305412807008256 
 + 
 +  * 62% "Love It" 
 +  * 24% "Don't Care" 
 +  * 14% "Hate It" 
 + 
 +==== In favor ==== 
 + 
 +  * Produces cleaner, more readable code, in order the things are executed 
 +  * Doesn't pollute local symbol table with intermediates of potentially varying types 
 +  * Enforces immutability and data transformation, less chances of bugs 
 + 
 +==== Against ==== 
 + 
 +  * The new tokens are inobvious and difficult to google for 
 +    * Pipe chaining in other languages follows different rules \\ (e.g. implicit first arg, rather than explicit placeholder) 
 +    * Potentially confusing with variable-variables 
 +  * No opportunity for error catching/handling 
 +  * Can be implemented using intermediate variables
  
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
-Adopt the PIpe Operator yes/no?  Requires a 2/3 + 1 majority.+Adopt the Pipe Operator yes/no?  Requires a 2/3 + 1 majority.
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
rfc/pipe-operator.1462331465.txt.gz · Last modified: 2017/09/22 13:28 (external edit)