rfc:mixed-typehint
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:mixed-typehint [2017/09/22 13:28] – external edit 127.0.0.1 | rfc:mixed-typehint [2020/07/22 09:03] (current) – kocsismate | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== PHP RFC: Mixed typehint ====== | + | ====== PHP RFC: Mixed typehint |
- | * Version: 1.0 | + | * 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 recently | + | |
+ | With the addition of scalar types in PHP 7, nullables in 7.1 and '' | ||
===== Proposal ===== | ===== Proposal ===== | ||
- | This RFC proposes to add the '' | ||
- | Primary motivation for having explicit | + | This RFC proposes to add the '' |
- | In PHP 7.2, mixed types are unfortunately | + | |
- | The behavior | + | ===== 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 ===== | ===== Nullability ===== | ||
- | As the '' | + | |
+ | As the '' | ||
<code php> | <code php> | ||
- | function foo(): ?mixed {} | + | function foo(?mixed $arg) {} // Fatal error: Mixed types cannot be nullable, null is already part of the mixed type. |
- | // Fatal error | + | |
+ | function bar() : ?mixed {} // Fatal error: Mixed types cannot be nullable, null is already part of the mixed type. | ||
</ | </ | ||
- | ===== Variance | + | ===== Mixed vs. Void vs. no return type ===== |
- | No changes to variance needed. Exactly same rules as when no type is present apply. | + | 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 |
- | Since the '' | + | ====== Inheritance ====== |
+ | |||
+ | ======= Parameters ======= | ||
+ | |||
+ | As parameters cannot be '' | ||
+ | |||
+ | The following code is valid: | ||
<code php> | <code php> | ||
- | function foo($arg) { | + | class A |
- | | + | { |
+ | public | ||
+ | {} | ||
+ | } | ||
+ | |||
+ | class B extends A | ||
+ | { | ||
+ | | ||
+ | {} | ||
+ | } | ||
+ | |||
+ | class C extends B | ||
+ | { | ||
+ | public function foo($value) // no type is specified, mixed type is assumed and conforms to parent declaration | ||
+ | {} | ||
} | } | ||
</ | </ | ||
- | and | + | |
+ | ======= Return types ======= | ||
+ | |||
+ | Since return types may be either '' | ||
<code php> | <code php> | ||
- | function foo(mixed | + | class A |
- | | + | { |
+ | public | ||
+ | {} | ||
+ | } | ||
+ | |||
+ | class B extends A | ||
+ | { | ||
+ | public function foo() : mixed // mixed type is explicitly specified, function must return some value | ||
+ | {} | ||
+ | } | ||
+ | |||
+ | class C extends B | ||
+ | { | ||
+ | | ||
+ | {} | ||
+ | } | ||
+ | |||
+ | class D extends B | ||
+ | { | ||
+ | public function foo() : void // void type is specified, as void is not subtype of mixed, Fatal error is thrown | ||
+ | {} | ||
} | } | ||
</ | </ | ||
- | Same applies | + | ===== Variance ===== |
+ | |||
+ | '' | ||
+ | - Parameter type may be widened in a subclass from a specific value type to the '' | ||
+ | - Specific return | ||
<code php> | <code php> | ||
- | class Foo { | + | class A |
- | public function | + | { |
- | | + | public function |
- | | + | |
+ | |||
+ | | ||
+ | {} | ||
} | } | ||
- | class Bar extends | + | |
- | public function | + | class B extends |
- | return parent:: | + | { |
- | } | + | public function |
- | } | + | |
- | class Baz extends bar { | + | |
- | public function | + | public function |
- | return parent:: | + | |
- | } | + | |
} | } | ||
</ | </ | ||
===== 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. | ||
As 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:// | ||
rfc/mixed-typehint.txt · Last modified: 2020/07/22 09:03 by kocsismate