rfc:dnf_types
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:dnf_types [2021/11/04 15:37] – Add variance. crell | rfc:dnf_types [2023/01/21 22:12] (current) – Typo in code example, brakets are not allowed for standalone intersection types girgias | ||
---|---|---|---|
Line 2: | Line 2: | ||
* Version: 0.9 | * Version: 0.9 | ||
* Date: 2021-11-04 | * Date: 2021-11-04 | ||
- | * Author: George Peter Banyard, girgias@php.net; | + | * Author: George Peter Banyard |
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
Line 37: | Line 37: | ||
// Accepts an object that implements both A and B, | // Accepts an object that implements both A and B, | ||
// OR an object that implements D. | // OR an object that implements D. | ||
- | A&B|D | + | (A&B)|D |
// Accepts an object that implements C, | // Accepts an object that implements C, | ||
// OR a child of X that also implements D, | // OR a child of X that also implements D, | ||
// OR null. | // OR null. | ||
- | C|X& | + | C|(X&D)|null |
// Accepts an object that implements all three of A, B, and D, | // Accepts an object that implements all three of A, B, and D, | ||
// OR an int, | // OR an int, | ||
// OR null. | // OR null. | ||
- | A& | + | (A& |
</ | </ | ||
Line 54: | Line 54: | ||
< | < | ||
A&(B|D) | A&(B|D) | ||
- | // Can be rewritten as A& | + | // Can be rewritten as (A&B)|(A&D) |
A|(B& | A|(B& | ||
- | // Can be rewritten as A|B& | + | // Can be rewritten as A|(B&D)|(B&W)|null |
</ | </ | ||
Line 63: | Line 63: | ||
< | < | ||
- | A& | + | (A&B)|(C&D)|(Y&D)|null |
- | B& | + | (B&A)|null|(D&Y)|(C&D) |
- | null|C& | + | null|(C&D)|(B&A)|(Y&D) |
</ | </ | ||
Requiring DNF for all type declarations allows conceptually all potential combinations of intersection and union rules, but in a standard fashion that is easier for the engine and easier for humans and static analyzers to comprehend. | Requiring DNF for all type declarations allows conceptually all potential combinations of intersection and union rules, but in a standard fashion that is easier for the engine and easier for humans and static analyzers to comprehend. | ||
+ | |||
+ | The parentheses around each intersection segment is required. | ||
==== Return co-variance ==== | ==== Return co-variance ==== | ||
Line 76: | Line 78: | ||
<code php> | <code php> | ||
interface ITest { | interface ITest { | ||
- | public function stuff(): A&B|D; | + | public function stuff(): |
} | } | ||
Line 109: | Line 111: | ||
// Thus this definition is wider, and not allowed. | // Thus this definition is wider, and not allowed. | ||
class TestFive implements ITestTwo { | class TestFive implements ITestTwo { | ||
- | public function things(): A&B|D {} | + | public function things(): |
} | } | ||
</ | </ | ||
Line 119: | Line 121: | ||
<code php> | <code php> | ||
interface ITest { | interface ITest { | ||
- | public function stuff(A& | + | public function stuff((A&B)|D $arg): void {} |
} | } | ||
Line 125: | Line 127: | ||
// and then some. | // and then some. | ||
class TestOne implements ITest { | class TestOne implements ITest { | ||
- | public function stuff(A& | + | public function stuff((A&B)|D|Z $arg): void {} |
} | } | ||
Line 137: | Line 139: | ||
// but this class does not. | // but this class does not. | ||
class TestOne implements ITest { | class TestOne implements ITest { | ||
- | public function stuff(A& | + | public function stuff((A&B) $arg): void {} |
} | } | ||
Line 148: | Line 150: | ||
// directly, and thus is wider. | // directly, and thus is wider. | ||
class TestFive implements ITestTwo { | class TestFive implements ITestTwo { | ||
- | public function things(A& | + | public function things((A&B)|D $arg): void; |
} | } | ||
</ | </ | ||
Line 155: | Line 157: | ||
Object properties are already invariant in inheritance. This RFC does not change that. The type of a redeclared child property must logically match its parent. | Object properties are already invariant in inheritance. This RFC does not change that. The type of a redeclared child property must logically match its parent. | ||
+ | |||
+ | ==== Duplicate and redundant types ==== | ||
+ | |||
+ | To catch some simple bugs in type declarations, | ||
+ | |||
+ | * Each segment of a DNF type must be unique. | ||
+ | |||
+ | A type declaration of '' | ||
+ | |||
+ | Note that a type declaration of '' | ||
+ | |||
+ | * Segments that are strict subsets of others are disallowed. | ||
+ | |||
+ | For example, the type definition '' | ||
+ | |||
+ | This does not guarantee that the type is " | ||
+ | |||
+ | ==== Reflection ==== | ||
+ | |||
+ | This RFC does not introduce any new reflection classes. | ||
+ | |||
+ | '' | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
- | None. | + | The sub-values of a '' |
===== Proposed PHP Version(s) ===== | ===== Proposed PHP Version(s) ===== | ||
Line 164: | Line 188: | ||
8.2 | 8.2 | ||
- | ===== Open Issues | + | ===== Vote ===== |
- | Make sure there are no open issues when the vote starts! | + | As per the voting RFC a yes/ |
- | ===== Unaffected PHP Functionality ===== | + | Voting started on 2022-06-17 and will end on 2022-07-01. |
- | + | <doodle title=" | |
- | List existing areas/features of PHP that will not be changed by the RFC. | + | * Yes |
- | + | * No | |
- | This helps avoid any ambiguity, shows that you have thought deeply about the RFC's impact, and helps reduces mail list noise. | + | </doodle> |
===== Future Scope ===== | ===== Future Scope ===== | ||
Line 178: | Line 202: | ||
==== Non-DNF types ==== | ==== Non-DNF types ==== | ||
- | In theory, supporting conjunctive normal form type definitions (and ANDed list of ORs) may be possible. | + | In theory, supporting conjunctive normal form type definitions (and ANDed list of ORs) or types which are not in a normalised form may be possible, either by supporting them directly or doing a compile time rewrite of the type expression. |
+ | |||
+ | However, as DNF is able to represent all reasonable boolean expressions | ||
==== Type aliasing ==== | ==== Type aliasing ==== | ||
Line 190: | Line 216: | ||
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
- | Patch is available here: https:// | + | Patch is available here: https:// |
===== Implementation ===== | ===== Implementation ===== |
rfc/dnf_types.1636040249.txt.gz · Last modified: 2021/11/04 15:37 by crell