rfc:mixed_type_v2
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:mixed_type_v2 [2020/04/20 01:05] – format danack | rfc:mixed_type_v2 [2020/05/07 08:56] – kocsismate | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: Mixed Type v2 ====== | ====== PHP RFC: Mixed Type v2 ====== | ||
- | * Version: 0.1 | + | * Version: 0.9 |
* Date: 2020-03-23 | * Date: 2020-03-23 | ||
* Author: Máté Kocsis < | * Author: Máté Kocsis < | ||
* Based on previous RFC by: Michael Moravec | * Based on previous RFC by: Michael Moravec | ||
- | | + | * Status: |
- | | + | * Implementation: |
- | * Implementation: | + | |
* First Published at: https:// | * First Published at: https:// | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | With the addition of scalar types in PHP 7, nullables in 7.1, `object` in 7.2, and lastly, union types in 8.0, people writing PHP code can explicitly declare type information for most function parameters, function returns, as well as class properties. | + | With the addition of scalar types in PHP 7, nullables in 7.1, object in 7.2, and lastly, union types in 8.0, people writing PHP code can explicitly declare type information for most function parameters, function returns, as well as class properties. |
However, PHP has not always supported types, and most probably it will always allow to omit type information. But this leads to the problem that its meaning is ambiguous when type information is missing: | However, PHP has not always supported types, and most probably it will always allow to omit type information. But this leads to the problem that its meaning is ambiguous when type information is missing: | ||
- | * the type is a specific type, but the programmer forgot to declare it. | + | |
- | * the type is a specific type, but the programmer omitted it to keep compatibility with an older PHP version | + | * the type is a specific type, but the programmer omitted it to keep compatibility with an older PHP version |
- | * the type is not currently expressible in PHP's type system, and so no type could be specified. | + | * the type is not currently expressible in PHP's type system, and so no type could be specified. |
- | * for return types, it is not clear if the function will or will not return a value, other than null. | + | * for return types, it is not clear if the function will or will not return a value, other than null. |
An explicit '' | An explicit '' | ||
Line 24: | Line 23: | ||
Currently, '' | Currently, '' | ||
- | One prominent example where the '' | + | One prominent example where the '' |
Additionally, | Additionally, | ||
Line 40: | Line 39: | ||
==== LSP, Covariance and Contravariance ==== | ==== LSP, Covariance and Contravariance ==== | ||
- | The proposal conforms to the [Liskov Substituion Priniciple](https:// | + | The proposal conforms to the [[https:// |
- | Since 7.4 PHP allows [covariant returns and contravariant parameters](https:// | + | Since 7.4 PHP allows [[https:// |
PHP allows contravariance (aka type widening) for parameter types to obey the LSP principle. A subclass may use a ' | PHP allows contravariance (aka type widening) for parameter types to obey the LSP principle. A subclass may use a ' | ||
Line 124: | Line 123: | ||
==== Property types are invariant ==== | ==== Property types are invariant ==== | ||
- | Following the [typed properties RFC](https:// | + | Following the [[typed properties RFC|https:// |
<code php> | <code php> | ||
Line 218: | Line 217: | ||
Currently this only affects inheritance in classes. | Currently this only affects inheritance in classes. | ||
- | If/when PHP gains the abilties to declare [function signatures as types](https:// | + | If/when PHP gains the abilties to declare [[https:// |
==== Signature checking of function when no return type present ==== | ==== Signature checking of function when no return type present ==== | ||
Line 272: | Line 271: | ||
//INVALID - Fatal error: Mixed types cannot be nullable, null is already part of the mixed type. | //INVALID - Fatal error: Mixed types cannot be nullable, null is already part of the mixed type. | ||
function bar(): ?mixed {} | function bar(): ?mixed {} | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ==== Explicit returns ==== | ||
+ | |||
+ | When using mixed as a return type, a value must be explicitly returned from the function, otherwise a TypeError will be thrown. | ||
+ | |||
+ | <code php> | ||
+ | function foo(): mixed {} | ||
+ | |||
+ | foo(); | ||
+ | |||
+ | // Uncaught TypeError: Return value of foo() must be of | ||
+ | // the type mixed, none returned | ||
+ | </ | ||
+ | |||
+ | This is consistent with the existing behaviour for other return types. | ||
+ | |||
+ | <code php> | ||
+ | function bar(): ?int {} | ||
+ | bar(); | ||
+ | // Uncaught TypeError: Return value of bar() must be of | ||
+ | // the type int or null, none returned | ||
+ | |||
</ | </ | ||
Line 284: | Line 308: | ||
This RFC proposes '' | This RFC proposes '' | ||
- | Also, choosing to use '' | + | Also, choosing to use '' |
Line 295: | Line 319: | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
- | Since PHP 7.0, '' | + | Since PHP 7.0, '' |
===== To SAPIs ===== | ===== To SAPIs ===== | ||
Line 306: | Line 330: | ||
Not analyzed. | Not analyzed. | ||
- | ===== Proposed Voting Choices | + | ===== Vote ===== |
+ | The vote starts on 2020-05-07 and ends on 2020-05-21 12:00 UTC. The vote requires 2/3 majority to be accepted. | ||
- | Add '' | + | <doodle title=" |
+ | * Yes | ||
+ | * No | ||
+ | </doodle> | ||
rfc/mixed_type_v2.txt · Last modified: 2020/05/22 14:22 by kocsismate