rfc:list_default_value

Differences

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

Link to this comparison view

Next revision
Previous revision
rfc:list_default_value [2015/11/07 17:38] – created reezerfc:list_default_value [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 1: Line 1:
-====== PHP RFC:  Default Value in List Syntax ====== +====== PHP RFC:  Default Value in List Assignment Syntax ====== 
-  * Version: 0.9+  * Version: 0.1
   * Date: 2015-11-08   * Date: 2015-11-08
   * Author: Reeze Xia, reeze@php.net   * Author: Reeze Xia, reeze@php.net
-  * Status: Draft+  * Status: Under Discussion
   * First Published at: http://wiki.php.net/rfc/list_default_value   * First Published at: http://wiki.php.net/rfc/list_default_value
  
-This is a suggested template for PHP Request for Comments (RFCs). Change this template to suit your RFC.  Not all RFCs need to be tightly specified.  Not all RFCs need all the sections below. +===== Introduction ===== 
-Read https://wiki.php.net/rfc/howto carefully!+We could destruct variables from an array with [[http://php.net/list| list constuct]], it may be nested or a simple array. But there is no guarantee that the array can fulfill all variables, it will be simple assigned with null with notice error (not good). We will need several redundant code to handle the possible cases if we want to check it and assign it with default values
  
 +For the similar reason we introduced  [[http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison|Null coalesce "??"]] and [[http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary|Ternary Operator "?:"]] to help write clean code.
  
-Quoting [[http://news.php.net/php.internals/71525|Rasmus]]:+Some clever users figured out  some workaround like this  [[http://php.net/manual/en/function.list.php#113189|User contributed notes]]. But that is not good enough and ugly.
  
-> PHP is and should remain: +===== Proposal =====
-> 1) a pragmatic web-focused language +
-> 2) a loosely typed language +
-> 3) a language which caters to the skill-levels and platforms of a wide range of users+
  
-Your RFC should move PHP forward following his vision. As [[http://news.php.net/php.internals/66065|said by Zeev Suraski]] "Consider only features which have significant traction to +Support new syntax to set default values for list elementswhen the requested index it is not found in array:
-large chunk of our userbaseand not something that could be useful in some +
-extremely specialized edge cases [...] Make sure you think about the full context, the huge audience out there, the consequences of  making the learning curve steeper with +
-every new feature, and the scope of the goodness that those new features bring."+
  
-===== Introduction ===== +<code php> 
-The elevator pitch for the RFC. The first paragraph in this section will be slightly larger to give it emphasisplease write good introduction.+// basic syntax 
 +list($a, $b='default'[1];      // a 1, b 'default' 
 +list($a, $b='default'[1, 2] // = 1, b = 2
  
-===== Proposal ===== +//  comparation  
-All the features and examples of the proposal.+list($a, list($b=1, $c=2)) $arr; 
 +// or we need to check it ourself 
 +if (!isset($arr[1][0])) { 
 +    $arr[1][0] 1; 
 +
 +if (!isset($arr[1][1])) { 
 +    $arr[1][0] 2; 
 +}
  
-To [[http://news.php.net/php.internals/66051|paraphrase Zeev Suraski]]explain hows the proposal brings substantial value to be considered +list($alist($b, $c)) = $arr;
-for inclusion in one of the world's most popular programming languages.+
  
-Remember that the RFC contents should be easily reusable in the PHP Documentation. 
  
-===== Backward Incompatible Changes ===== +// other examples 
-What breaksand what is the justification for it?+function say_hello() 
 +
 +    return "Hello"; 
 +
 +$name 'PHP'; 
 +list($a=say_hello(), $b=$name."7.0"[]; // a 'Hello'b = 'PHP7.0'
  
-===== Proposed PHP Version(s) ====+list($a, list($b=1, $c=2)) = [1]; // a 1, b 1, c 2
-List the proposed PHP versions that the feature will be included in.  Use relative versions such as "next PHP 5.x" or "next PHP 5.x.y".+
  
-===== RFC Impact ===== +</code>
-==== To SAPIs ==== +
-Describe the impact to CLI, Development web server, embedded PHP etc.+
  
-==== To Existing Extensions ==== +The assignment could be considered as a shortcut for:
-Will existing extensions be affected?+
  
-==== To Opcache ==== +<code php>
-It is necessary to develop RFC's with opcache in mind, since opcache is a core extension distributed with PHP.+
  
-Please explain how you have verified your RFC's compatibility with opcache.+list($a, $b='default') = $arr;
  
-==== New Constants ==== +// equals
-Describe any new constants so they can be accurately and comprehensively explained in the PHP documentation.+
  
-==== php.ini Defaults ==== +$a $arr[0]; 
-If there are any php.ini settings then list: +$b isset($arr[1]) ? $arr[1] 'default'; 
-  * hardcoded default values +</code>
-  * php.ini-development values +
-  * php.ini-production values+
  
-===== Open Issues ===== +This seems not a lot of work than the new syntax. But sometimes we need to destruct nested array and there are morn than two variables, anyway list() itself is designed to avoid assign by hand for short and clean code.
-Make sure there are no open issues when the vote starts!+
  
-===== Unaffected PHP Functionality ===== 
-List existing areas/features of PHP that will not be changed by the RFC. 
  
-This helps avoid any ambiguity, shows that you have thought deeply about the RFC'impactand helps reduces mail list noise.+The syntax is: 
 + 
 +<code flex> 
 +assignment_list_element: 
 + variable  
 ++ | variable '=' expr # the new syntax. value could be any expression 
 + | T_LIST '(' assignment_list ')' 
 + | /* empty */  
 +</code> 
 + 
 +The syntax is same as function'default value for parameters. 
 + 
 + 
 +We could also set default value in foreach list context: 
 + 
 +<code php> 
 +foreach($arr as list($k, list($v1='deafult', $v2='default2'))) { 
 +    // do something 
 +
 +</code> 
 + 
 +More case could be found from PR's tests:  [[https://github.com/php/php-src/pull/1623/files]] 
 + 
 +This feature could also be found in [[https://clojurebridge.github.io/community-docs/docs/clojure/destructuring/|Clojure]] and [[https://hacks.mozilla.org/2015/05/es6-in-depth-destructuring/|Javascript]] for destructing.  
 + 
 +===== Backward Incompatible Changes ===== 
 + 
 +No BC break. 
 + 
 +===== Proposed PHP Version(s) ===== 
 +PHP 7.1 
 + 
 +===== RFC Impact ===== 
 + 
 +==== To Opcache ==== 
 + 
 +I am working on opcache compatibility. 
 + 
 +===== Open Issues ===== 
 +None for now
  
 ===== Future Scope ===== ===== Future Scope =====
-This sections details areas where the feature might be improved in future, but that are not currently proposed in this RFC. 
  
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
-Include these so readers know where you are heading and can discuss the proposed voting options. 
  
-State whether this project requires a 2/3 or 50%+1 majority (see [[voting]])+* Whether accept the RFC for PHP 7.1 
 + 
 +This project requires a 2/3 majority (see [[voting]])
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
-Links to any external patches and tests go here. 
  
-If there is no patch, make it clear who will create a patch, or whether a volunteer to help with implementation is needed.+* Patch: [[https://github.com/php/php-src/pull/1623]]
  
-Make it clear if the patch is intended to be the final patch, or is just a prototype.+I am working on opcache compatibility
  
 ===== Implementation ===== ===== Implementation =====
Line 90: Line 124:
  
 ===== References ===== ===== References =====
-Links to external references, discussions or RFCs+ 
 +  * Javascript destructing:  [[https://hacks.mozilla.org/2015/05/es6-in-depth-destructuring/]] 
 +  * Clojure destructing [[https://clojurebridge.github.io/community-docs/docs/clojure/destructuring/]]
  
 ===== Rejected Features ===== ===== Rejected Features =====
-Keep this updated with features that were discussed on the mail lists.+ 
 +===== Changelog ===== 
 + 
 +  * v0.1 - Initial version
rfc/list_default_value.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1