rfc:intersection_types

Differences

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

Link to this comparison view

Next revision
Previous revision
Next revisionBoth sides next revision
rfc:intersection_types [2016/04/27 17:23] – created levimrfc:intersection_types [2016/04/28 14:21] – Add 3v4l.org links levim
Line 3: Line 3:
   * Date: 2016-04-27   * Date: 2016-04-27
   * Author: Levi Morrison <levim@php.net>   * Author: Levi Morrison <levim@php.net>
-  * Status: Draft+  * Status: Under Discussion
   * First Published at: http://wiki.php.net/rfc/intersection_types   * First Published at: http://wiki.php.net/rfc/intersection_types
  
Line 12: Line 12:
 Intersection types allow the programmer to write multiple type declarations for a parameter or return value. The value must satisfy all of the declared types. Each type is separated by ampersand (''&''). Intersection types allow the programmer to write multiple type declarations for a parameter or return value. The value must satisfy all of the declared types. Each type is separated by ampersand (''&'').
  
-Here is a practical example:+Here is a practical example. Given this definition:
  
 <PHP> <PHP>
Line 28: Line 28:
 } }
 </PHP> </PHP>
 +
 +This will error ([[https://3v4l.org/qCeXi/rfc#rfc-multi-types|see on 3v4l.org]]):
 +<PHP>
 +// Argument 1 passed to RecordsToList() must be Countable and Traversable, array given
 +echo RecordsToList(["Lennon", "McCartney", "Starr", "Harrison"]);
 +</PHP>
 +
 +This works correctly ([[https://3v4l.org/7GvqN/rfc#rfc-multi-types|see on 3v4l]]):
 +<PHP>
 +echo RecordsToList(new ArrayObject(["Lennon", "McCartney", "Starr", "Harrison"]));
 +// Output:
 +// <ol>
 +//     <li>Lennon</li>
 +//     <li>McCartney</li>
 +//     <li>Starr</li>
 +//     <li>Harrison</li>
 +// </ol>
 +</PHP>
 +
 +Note that if [[rfc:union_types|Union Types]] are also accepted then something like this will be possible, allowing for an array or an object that satisfies both Countable and Traversable:
 +<PHP>
 +function RecordsToList(Array | (Countable & Traversable) $input): String {
 +    if (count($input) > 0) {
 +        $output = "<ol>\n";
 +        foreach ($input as $value) {
 +            $output .= "\t<li>" . htmlentities($value) . "</li>\n";
 +        }
 +        $output .= "</ol>\n";
 +        return $output;
 +    } else {
 +        return "<p>No items to display.</p>\n";
 +    }
 +}
 +</PHP>
 +
 +When union and intersection types are in the same expression they must be grouped with parenthesis (demonstrated above). The following is invalid:
 +
 +<PHP>
 +function RecordsToList(Array | Countable & Traversable): String {
 +    // ...
 +}
 +</PHP>
 +
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
Line 47: Line 90:
  
 ===== References ===== ===== References =====
-Original announcement on Mailing List of rationale for this feature: http://news.php.net/php.internals/92252+  * Original announcement on Mailing List of rationale for this feature: http://news.php.net/php.internals/92252 
 +  * Announcement for discussion phase: http://news.php.net/php.internals/92857
  
rfc/intersection_types.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1