rfc:iterable

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:iterable [2016/06/11 14:54]
trowski
rfc:iterable [2018/08/07 01:40] (current)
carusogabriel Change status to: Implemented in PHP 7.1
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: Under Discussion+  * 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 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.1465656877.txt.gz · Last modified: 2017/09/22 13:28 (external edit)