rfc:iterable

Differences

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

Link to this comparison view

Next revision
Previous revision
rfc:iterable [2016/06/10 23:15] – created trowskirfc:iterable [2018/08/07 01:40] (current) – Change status to: Implemented in PHP 7.1 carusogabriel
Line 1: Line 1:
 ====== PHP RFC: Iterable ====== ====== PHP RFC: Iterable ======
-  * Version: 0.1+  * Version: 0.1.1
   * Date: 2016-06-10   * Date: 2016-06-10
   * Author: Aaron Piotrowski <aaron@trowski.com>   * Author: Aaron Piotrowski <aaron@trowski.com>
-  * Status: Draft+  * Status: Implemented in PHP 7.1
   * First Published at: http://wiki.php.net/rfc/iterable   * First Published at: http://wiki.php.net/rfc/iterable
  
Line 32: Line 32:
 </code> </code>
  
-Parameters declared as ''iterable'' may use ''null'' or an empty array as a default value.+Parameters declared as ''iterable'' may use ''null'' or an array as a default value.
  
 <code php> <code php>
Line 47: Line 47:
     yield 2;     yield 2;
     yield 3;     yield 3;
 +}
 +</code>
 +
 +Classes extending/implementing may broaden methods using ''array'' or ''Traversable'' as parameter types to ''iterable'' or narrow return types from ''iterable'' to ''array'' or ''Traversable''. This behavior is the same as that proposed for all union types in the [[union_types|Union Types RFC]].
 +
 +<code php>
 +interface Example {
 +    public function method(array $array): iterable;
 +}
 +
 +class ExampleImplementation implements Example {
 +    public function method(iterable $iterable): array {
 +        // Parameter broadened and return narrowed.
 +    }
 } }
 </code> </code>
Line 59: Line 73:
 var_dump(is_iterable(new stdClass())); // bool(false) var_dump(is_iterable(new stdClass())); // bool(false)
 </code> </code>
 +
 +===== Object Iteration =====
 +
 +PHP allows any object to be used with ''foreach''. However, ''iterable'' does not accept any object, only those implementing ''Traversable''. Values accepted by ''iterable'' should be designed for iteration, not any set of values (such as the public properties of an object or a string). There is already some precedent in the language for discriminating between values accepted by ''iterable'' and those that can be used with ''foreach''. ''yield from'' only accepts arrays or objects implementing ''Traversable''. ''iterable'' then represents the values usable with ''yield from''.
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
Line 75: Line 93:
 ===== Voting ===== ===== Voting =====
 This proposal requires a 2/3 majority to be accepted. This proposal requires a 2/3 majority to be accepted.
 +
 +Voting began on 2016-06-24 at 6:00 pm GMT and ended on 2016-07-02 at 11:59 pm GMT
 +
 +<doodle title="Add iterable type to PHP 7.1" auth="trowski" voteType="single" closed="true">
 +   * Yes
 +   * No
 +</doodle>
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
 PR: [[https://github.com/php/php-src/pull/1941|#1941]] PR: [[https://github.com/php/php-src/pull/1941|#1941]]
- 
rfc/iterable.1465600558.txt.gz · Last modified: 2017/09/22 13:28 (external edit)