rfc:arrayof

Differences

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

Link to this comparison view

Next revision
Previous revision
rfc:arrayof [2014/01/15 18:10] – created philsturfc:arrayof [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 1: Line 1:
  
 ====== PHP RFC: Array Of ====== ====== PHP RFC: Array Of ======
-  * Version: 0.1.0+  * Version: 0.2.0
   * Date: 2014-01-15   * Date: 2014-01-15
   * Author: Joe Watkins, krakjoe@php.net & Phil Sturgeon philstu@php.net   * Author: Joe Watkins, krakjoe@php.net & Phil Sturgeon philstu@php.net
-  * Status: Under Discussion+  * Status: Declined
   * First Published at: http://wiki.php.net/rfc/arrayof   * First Published at: http://wiki.php.net/rfc/arrayof
  
Line 21: Line 21:
 </code> </code>
  
-The square brackets allow type-hinting for an array where each value is of a specific type. This is handy when simply type-hinting with "array" is just not clear enough what the content should be. It improves readability and self-documentation of the signature, and avoids asking users to forech through the argument to calculate if each item is of the expected type.+The square brackets allow type-hinting for an array where each value is of a specific type. This is handy when simply type-hinting with "array" is just not clear enough what the content should be. It improves readability and self-documentation of the signature, and avoids asking users to foreach through the argument to verify if each item is of the expected type. 
 + 
 +Default values can still be used: 
 + 
 +<code php> 
 +function test(SplFileObject[] $files = null) { 
 +    var_dump($files); 
 +
 +test(); // Outputs: null 
 +</code>
  
 Interfaces and instances work in the same way as expected for existing type-hints: Interfaces and instances work in the same way as expected for existing type-hints:
Line 109: Line 118:
 ===== Unaffected PHP Functionality ===== ===== Unaffected PHP Functionality =====
  
-This RFC has no effect on [[https://wiki.php.net/rfc/variadics|variadics]], or [[https://wiki.php.net/rfc/named_params|named parameters]]. It does not alter other type-hinting rules in regards to what can be hinted against (callable, array, classes, interface, traits, etc) and does not attempt to type-hint against scalar values.+This RFC has no effect on [[https://wiki.php.net/rfc/variadics|variadics]], or [[https://wiki.php.net/rfc/named_params|named parameters]]. It does not alter other type-hinting rules in regards to what can be hinted against (callable, array, classes, interface, traits, etc) and does not attempt to type-hint against scalar values. This is being taken care of in [[https://wiki.php.net/rfc/scalar_type_hinting_with_cast|Scalar Type Hinting with Case]]
  
 This also does not attempt to allow Traversable or other objects implementing interfaces to be considered an "array", to match current behavior with hinting for an array. This also does not attempt to allow Traversable or other objects implementing interfaces to be considered an "array", to match current behavior with hinting for an array.
 +
 +===== Closed Questions =====
 +
 +1. Should multi-dimensional hints be allowed?
 +
 +<code php>
 +function test(Foo[][] $files = null) {}
 +</code>
 +
 +The general consensus seemed to be that instead of using "function (ChessPieces[][])" a better solution would probably to simply use "function (Black[] $pieces, White[] $pieces)" instead.
 +
 +
 +2. RFC suggested syntax, or Hack Generics syntax?
 +
 +With this features close proximity to the "generics" feature found in other languages - including Java, C# and Hack - the Hack syntax was suggested to replace the syntax in this RFC, which would be the first half of a larger effort in a later version to introduce full-generics. A survey was held to see if people were interested in going down the route of generics and the answer was mostly "Yes, but we want this Array Of too".
 +
 +http://grokbase.com/p/php/php-internals/141rva4cf7/php-dev-vote-array-of-v-generics
 +
 +Java has both features, with their own syntax. This means there is no reason for us to hold off implementing with the existing syntax and looking into adding generics as well, at a later date. 
 +
 +If people want to change the syntax of this feature more in line with Hack generics syntax then simply vote no, and we can revisit the issue.
 +
 +3. The RFC is currently //effectively// the same as the code below:
 +
 +<code php>
 +foreach ($foos as $foo) {
 +    if (! $foo instanceof Face) {
 +        throw new Exception ('AAAGGGGGHHH!');
 +    }
 +}
 +</code>
 +
 +Some have suggested they would prefer nulls to be allowed in the array, so the syntax would instead represent the following:
 +
 +<code php>
 +foreach ($foos as $foo) {
 +    if (! is_null($foo) and ! $foo instanceof Face) {
 +        throw new Exception ('AAAGGGGGHHH!');
 +    }
 +}
 +</code>
 +
 +The downside here is that before anyone can confidently interact with a type array of what one would assume are instances, they need to do a array_filter() first. 
 +
 +Logic here would dictate that if you ask for a bag of spanners, you get a bag of spanners, not a bag with a few spanners and maybe a few  "I owe you one spanner" notes.
 +
 +Multiple people suggested that by default allowing nulls would make this feature useless, and that syntax could be added for the alternative. This syntax can be addressed in a later RFC but could be as simple as function (Foo[]? $foos). Undecided at this point.
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
Line 120: Line 176:
  
 PHP 5.6 PHP 5.6
 +
 +===== Vote =====
 +
 +VOTE: 2014/02/28 - 2014/03/07
 +
 +<doodle title="Array Of Type Hinting" auth="philstu" voteType="single" closed="true">
 +   * Yes
 +   * No
 +</doodle>
 +
  
 ===== Implementation ===== ===== Implementation =====
Line 127: Line 193:
 [[https://github.com/php/php-src/pull/562/commits|Pull Request]] [[https://github.com/php/php-src/pull/562/commits|Pull Request]]
  
-===== References =====+===== Performance =====
  
 +https://gist.github.com/krakjoe/8444591
 +
 +===== References =====
  
 +[[http://grokbase.com/t/php/php-internals/141f1kzdm8/introducing-array-of-rfc|ML Discussion]]
 +[[http://grokbase.com/p/php/php-internals/141raagbec/php-dev-vote-array-of-v-generics|Array Of v Generics survey]]
rfc/arrayof.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1