rfc:mixed-typehint
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
rfc:mixed-typehint [2018/07/01 16:16] – Fix void* type carusogabriel | rfc:mixed-typehint [2020/07/22 09:03] – kocsismate | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: Mixed typehint ====== | ====== PHP RFC: Mixed typehint ====== | ||
- | * Version: 1.1 | + | * 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 '' | + | This RFC proposes to add the '' |
- | Primary motivation for having explicit mixed type is consistence and easier static analysis. | + | ===== Motivation ===== |
- | In PHP 7.2, mixed types are unfortunately the only type that could not be type hinted upon (and resource as well, but its future is unclear). Having mixed type would allow a code with 100% type coverage in most cases. | + | |
- | The behavior | + | 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 | ||
+ | - 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 ===== |
+ | |||
+ | 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 ======= | ||
- | No changes to variance needed. Exactly same rules as when no type is present | + | As parameters cannot be '' |
- | Since the '' | + | The following code is valid: |
<code php> | <code php> | ||
- | function foo($arg) { | + | class A |
- | | + | { |
+ | public | ||
+ | | ||
} | } | ||
- | </ | + | |
- | and | + | class B extends A |
- | <code php> | + | { |
- | function foo(mixed $arg): mixed { | + | |
- | | + | {} |
+ | } | ||
+ | |||
+ | class C extends B | ||
+ | { | ||
+ | | ||
+ | {} | ||
} | } | ||
</ | </ | ||
- | Same rules apply to inheritance, | + | ======= Return types ======= |
+ | |||
+ | Since return types may be either '' | ||
<code php> | <code php> | ||
- | class Foo { | + | class A |
- | public function | + | { |
- | | + | public function |
- | } | + | {} |
} | } | ||
- | class Bar extends | + | |
- | public function | + | class B extends |
- | | + | { |
- | } | + | public function |
+ | | ||
} | } | ||
- | class Baz extends | + | |
- | public function | + | class C extends |
- | | + | { |
- | } | + | public function |
+ | | ||
+ | } | ||
+ | |||
+ | class D extends B | ||
+ | { | ||
+ | public function foo() : void // void type is specified, as void is not subtype of mixed, Fatal error is thrown | ||
+ | | ||
} | } | ||
</ | </ | ||
- | ===== Mixed vs. Void ===== | + | ===== Variance |
- | As of PHP 7.1, PHP has a special | + | '' |
+ | - 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 '' | ||
- | In some other languages like C/C++ '' | + | <code php> |
- | This essentially matches the behavior of '' | + | class A |
+ | { | ||
+ | public function foo(int $value) | ||
+ | {} | ||
+ | |||
+ | public function bar() : mixed | ||
+ | {} | ||
+ | } | ||
- | As void is not a real type in PHP, it is not part of '' | + | class B extends A |
- | Should there ever be a type that includes all types //and// '' | + | { |
+ | public function foo(mixed $value) // parameter | ||
+ | {} | ||
+ | |||
+ | public function bar() : int // return type was narrowed from mixed to int, this is allowed | ||
+ | {} | ||
+ | } | ||
+ | </ | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
- | None, '' | + | None, '' |
- | Variance behavior is same as when no type is specified. | + | |
===== Proposed PHP Version(s) ===== | ===== Proposed PHP Version(s) ===== | ||
- | 7.3 | + | 7.4 |
===== RFC Impact ===== | ===== RFC Impact ===== | ||
+ | |||
==== To SAPIs ==== | ==== To SAPIs ==== | ||
Line 93: | Line 168: | ||
==== To Opcache ==== | ==== To Opcache ==== | ||
- | Not analyzed, likely none (no changes to current language | + | Not analyzed, likely none (no changes to current language |
===== Unaffected PHP Functionality ===== | ===== Unaffected PHP Functionality ===== |
rfc/mixed-typehint.txt · Last modified: 2020/07/22 09:03 by kocsismate