rfc:intersection_types

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:intersection_types [2016/04/28 03:55]
levim
rfc:intersection_types [2017/09/22 13:28] (current)
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 27: Line 27:
     }     }
 } }
 +</​PHP>​
  
-// Fatal error: Uncaught TypeError: Argument 1 passed to RecordsToList() must be Countable and Traversable,​ array given, called in %s on line %d+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"​]);​ echo RecordsToList(["​Lennon",​ "​McCartney",​ "​Starr",​ "​Harrison"​]);​
 +</​PHP>​
  
-// Fine:+This works correctly ([[https://​3v4l.org/7GvqN/rfc#​rfc-multi-types|see on 3v4l]]): 
 +<PHP>
 echo RecordsToList(new ArrayObject(["​Lennon",​ "​McCartney",​ "​Starr",​ "​Harrison"​]));​ echo RecordsToList(new ArrayObject(["​Lennon",​ "​McCartney",​ "​Starr",​ "​Harrison"​]));​
 // Output: // Output:
Line 40: Line 45:
 //     <​li>​Harrison</​li>​ //     <​li>​Harrison</​li>​
 // </ol> // </ol>
- 
 </​PHP>​ </​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:​+Note that if [[rfc:​union_types|Union Types]] are also accepted then something like this will be possible((The experimental patch provided by Joe and Bob currently does not support mixing unions and intersections)), allowing for an array or an object that satisfies both Countable and Traversable:​
 <PHP> <PHP>
 function RecordsToList(Array | (Countable & Traversable) $input): String { function RecordsToList(Array | (Countable & Traversable) $input): String {
Line 62: Line 66:
  
 <PHP> <PHP>
-function RecordsToList(Array | Countable & Traversable):​ String {+function RecordsToList(Array | Countable & Traversable ​$input): String {
     // ...     // ...
 } }
rfc/intersection_types.1461815732.txt.gz · Last modified: 2017/09/22 13:28 (external edit)