rfc:mixed-typehint
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
rfc:mixed-typehint [2017/07/19 21:57] – created majkl | rfc:mixed-typehint [2020/07/22 09:03] (current) – kocsismate | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== PHP RFC: Mixed typehint ====== | + | ====== PHP RFC: Mixed typehint |
- | * Version: | + | * Version: 1.2 |
* Date: 2017-07-19 | * Date: 2017-07-19 | ||
+ | * Revived: 2019-02-07 | ||
* Author: Michael Moravec (php.net@majkl578.cz) | * Author: Michael Moravec (php.net@majkl578.cz) | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
+ | * Superseded by: [[rfc: | ||
===== Introduction ===== | ===== Introduction ===== | ||
+ | With the addition of scalar types in PHP 7, nullables in 7.1 and '' | ||
===== Proposal ===== | ===== Proposal ===== | ||
+ | This RFC proposes to add the '' | ||
+ | |||
+ | ===== Motivation ===== | ||
+ | |||
+ | When no native type is present, it is unclear what type is expected - it could mean one of the following: | ||
+ | - the value is mixed, and since '' | ||
+ | - the value is of specific type, but native type is omitted due to compatibility with older PHP version; | ||
+ | - the value is of specific type, but the native type was forgotten. | ||
+ | |||
+ | At the time of writing it is not possible to differentiate between the abovementioned scenarios. | ||
+ | |||
+ | Additionally, | ||
+ | |||
+ | ===== Mixed and Void ===== | ||
+ | |||
+ | As of PHP 7.1, PHP has a special '' | ||
+ | |||
+ | The difference between '' | ||
+ | - Mixed means **any** value is returned. | ||
+ | - Void means **no** value is returned. | ||
+ | |||
+ | Due to this difference, '' | ||
+ | |||
+ | ===== Type system hierarchy ===== | ||
+ | |||
+ | < | ||
+ | * | ||
+ | | | ||
+ | |---------------------------|---------------------------| | ||
+ | | | | ||
+ | < | ||
+ | | | ||
+ | | ||
+ | | ||
+ | < | ||
+ | </ | ||
+ | |||
+ | ===== Nullability ===== | ||
+ | |||
+ | As the '' | ||
+ | |||
+ | <code php> | ||
+ | function foo(?mixed $arg) {} // Fatal error: Mixed types cannot be nullable, null is already part of the mixed type. | ||
+ | |||
+ | function bar() : ?mixed {} // Fatal error: Mixed types cannot be nullable, null is already part of the mixed type. | ||
+ | </ | ||
+ | |||
+ | ===== Mixed vs. Void vs. no return type ===== | ||
+ | |||
+ | When a function does not have a native return type, it means it //either// returns some value //or// does not return any value. More formally, this would be expressed as '' | ||
+ | |||
+ | ====== Inheritance ====== | ||
+ | |||
+ | ======= Parameters ======= | ||
+ | |||
+ | As parameters cannot be '' | ||
+ | |||
+ | The following code is valid: | ||
+ | <code php> | ||
+ | class A | ||
+ | { | ||
+ | public function foo($value) // no type is specified, mixed type is assumed | ||
+ | {} | ||
+ | } | ||
+ | |||
+ | class B extends A | ||
+ | { | ||
+ | public function foo(mixed $value) // mixed type is explicitly specified, conforming to parent declaration | ||
+ | {} | ||
+ | } | ||
+ | |||
+ | class C extends B | ||
+ | { | ||
+ | public function foo($value) // no type is specified, mixed type is assumed and conforms to parent declaration | ||
+ | {} | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ======= Return types ======= | ||
+ | |||
+ | Since return types may be either '' | ||
+ | |||
+ | <code php> | ||
+ | class A | ||
+ | { | ||
+ | public function foo() // no type is specified, mixed|void is assumed | ||
+ | {} | ||
+ | } | ||
+ | |||
+ | class B extends A | ||
+ | { | ||
+ | public function foo() : mixed // mixed type is explicitly specified, function must return some value | ||
+ | {} | ||
+ | } | ||
+ | |||
+ | class C extends B | ||
+ | { | ||
+ | public function foo() // no type is specified, mixed|void is assumed which is incompatible with mixed type only - Fatal error is thrown | ||
+ | {} | ||
+ | } | ||
+ | |||
+ | class D extends B | ||
+ | { | ||
+ | public function foo() : void // void type is specified, as void is not subtype of mixed, Fatal error is thrown | ||
+ | {} | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Variance ===== | ||
+ | |||
+ | '' | ||
+ | - Parameter type may be widened in a subclass from a specific value type to the '' | ||
+ | - Specific return type could be narrowed in a subclass by using the '' | ||
+ | |||
+ | <code php> | ||
+ | class A | ||
+ | { | ||
+ | public function foo(int $value) | ||
+ | {} | ||
+ | | ||
+ | public function bar() : mixed | ||
+ | {} | ||
+ | } | ||
+ | |||
+ | class B extends A | ||
+ | { | ||
+ | public function foo(mixed $value) // parameter type was widened from int to mixed, this is allowed | ||
+ | {} | ||
+ | | ||
+ | public function bar() : int // return type was narrowed from mixed to int, this is allowed | ||
+ | {} | ||
+ | } | ||
+ | </ | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
- | None. | + | |
+ | None, '' | ||
===== Proposed PHP Version(s) ===== | ===== Proposed PHP Version(s) ===== | ||
- | 7.2 if RMs agree, otherwise 7.3 | + | |
+ | 7.4 | ||
===== RFC Impact ===== | ===== RFC Impact ===== | ||
+ | |||
==== To SAPIs ==== | ==== To SAPIs ==== | ||
+ | |||
None. | None. | ||
==== To Existing Extensions ==== | ==== To Existing Extensions ==== | ||
+ | |||
None. | None. | ||
==== To Opcache ==== | ==== To Opcache ==== | ||
- | None. | + | |
+ | Not analyzed, likely none (no changes to current language behaviour). | ||
===== Unaffected PHP Functionality ===== | ===== Unaffected PHP Functionality ===== | ||
+ | |||
No changes to type cast operators. | No changes to type cast operators. | ||
===== Proposed Voting Choices ===== | ===== Proposed Voting Choices ===== | ||
+ | |||
Simple yes/no vote to either accept or reject addition of mixed type. | Simple yes/no vote to either accept or reject addition of mixed type. | ||
- | Since this is a language change, 2/3 majority is required. | + | As this is a language change, 2/3 majority is required. |
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
+ | |||
[[https:// | [[https:// | ||
===== References ===== | ===== References ===== | ||
- | [[https:// | + | * [[https:// |
+ | * [[https:// |
rfc/mixed-typehint.1500501462.txt.gz · Last modified: 2017/09/22 13:28 (external edit)