rfc:intersection_types
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:intersection_types [2016/04/27 17:28] – levim | rfc:intersection_types [2017/09/22 13:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 3: | Line 3: | ||
* Date: 2016-04-27 | * Date: 2016-04-27 | ||
* Author: Levi Morrison < | * Author: Levi Morrison < | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
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: | ||
} | } | ||
} | } | ||
+ | </ | ||
- | // Fatal error: Uncaught TypeError: Argument 1 passed to RecordsToList() must be Countable and Traversable, | + | This will error ([[https://3v4l.org/ |
- | echo RecordsToHtml([" | + | < |
+ | // Argument 1 passed to RecordsToList() must be Countable and Traversable, | ||
+ | echo RecordsToList([" | ||
+ | </ | ||
- | // Fine: | + | This works correctly ([[https://3v4l.org/ |
- | echo RecordsToHtml(new ArrayObject([" | + | <PHP> |
+ | echo RecordsToList(new ArrayObject([" | ||
// Output: | // Output: | ||
// <ol> | // <ol> | ||
Line 40: | Line 45: | ||
// < | // < | ||
// </ol> | // </ol> | ||
- | |||
</ | </ | ||
- | Note that if [[rfc: | + | Note that if [[rfc: |
<PHP> | <PHP> | ||
function RecordsToList(Array | (Countable & Traversable) $input): String { | function RecordsToList(Array | (Countable & Traversable) $input): String { | ||
Line 58: | Line 62: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | 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 $input): String { | ||
+ | // ... | ||
+ | } | ||
+ | </ | ||
+ | |||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
Line 77: | Line 90: | ||
===== References ===== | ===== References ===== | ||
- | Original announcement on Mailing List of rationale for this feature: http:// | + | * Original announcement on Mailing List of rationale for this feature: http:// |
+ | * Announcement for discussion phase: http:// | ||
rfc/intersection_types.1461778096.txt.gz · Last modified: 2017/09/22 13:28 (external edit)