rfc:union_types
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:union_types [2016/06/14 14:24] – bwoebi | rfc:union_types [2017/09/22 13:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 3: | Line 3: | ||
* Date: 2015-02-14 | * Date: 2015-02-14 | ||
* Author: Levi Morrison < | * Author: Levi Morrison < | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | In PHP using "type hints" to define either the allowed parameter types for a function, or the return | + | PHP has [[http:// |
- | * Using types allows | + | * They allow the PHP engine to enforce the correct type of variable passed to or returned from a function. |
- | * Using types makes it easy to reason about what types need to be passed to, or can be returned from a function. | + | * They make it easier |
- | For a lot of functions in PHP, each parameter will only be one type. Similarly, for the majority of functions, the return value of a function, | + | For a lot of functions in PHP each parameter will only be one type. Similarly, for the majority of functions the return value will only ever be of one type. |
- | However, for a significant number of functions, the acceptable parameters, or the possible return values, can be of more than one type. For example consider the '' | + | However, for a significant number of functions, the acceptable parameters or the possible return values can be of more than one type. For example consider the '' |
* if the needle exists it returns an integer. | * if the needle exists it returns an integer. | ||
Line 21: | Line 21: | ||
In the documentation on php.net, the two possible return types are documented as '' | In the documentation on php.net, the two possible return types are documented as '' | ||
- | Currently in userland code, when a parameter for a function can be one of a multiple | + | Currently in userland code when a parameter for a function can be one of multiple types, or the return value from a function can be one of multiple types, there can be no type information supplied. It is not possible for the PHP engine to enforce any types passed to/ |
- | This RFC seeks to address | + | This RFC seeks to address |
- | ==== Proposal ==== | + | ===== Proposal |
This RFC proposes the ability to define multiple possible types for parameter and return types. To define a 'union type' a single vertical bar (OR) is placed between types e.g. '' | This RFC proposes the ability to define multiple possible types for parameter and return types. To define a 'union type' a single vertical bar (OR) is placed between types e.g. '' | ||
Line 32: | Line 32: | ||
There can be more than two types in the union. | There can be more than two types in the union. | ||
- | === Parameter type examples === | + | ==== Parameter type examples |
A function that requires either a string or an array is passed to it as the parameter: | A function that requires either a string or an array is passed to it as the parameter: | ||
<PHP> | <PHP> | ||
Line 59: | Line 59: | ||
For this example, it is clear to both static analysis tools and humans that passing anything other than a ParameterGenerator object or a string to this function, would be an error. | For this example, it is clear to both static analysis tools and humans that passing anything other than a ParameterGenerator object or a string to this function, would be an error. | ||
- | === Return type example === | + | ==== Return type example |
A userland definition of '' | A userland definition of '' | ||
Line 166: | Line 166: | ||
This RFC requires that two-thirds of voters vote in favor of the RFC to pass. | This RFC requires that two-thirds of voters vote in favor of the RFC to pass. | ||
- | <doodle title=" | + | <doodle title=" |
* Yes | * Yes | ||
* No | * No | ||
Line 173: | Line 173: | ||
Additionally, | Additionally, | ||
- | <doodle title=" | + | <doodle title=" |
* Yes | * Yes | ||
* No | * No | ||
</ | </ | ||
| | ||
- | <doodle title=" | + | <doodle title=" |
* Yes | * Yes | ||
* No | * No |
rfc/union_types.1465914249.txt.gz · Last modified: 2017/09/22 13:28 (external edit)