rfc:true-type

This is an old revision of the document!


PHP RFC: Add true type

Introduction

PHP now has support for ''null'' and ''false'' as standalone types. However, true which is the natural counter part of false does not even exist as a type.

The motivations are the same to previous RFC targeting the type system:

  • Type system completeness
  • Providing precise type information while satisfying LSP

Proposal

Add support for using true as a type declaration, wherever type declarations are currently allowed.

class Truthy {
    public true $truthy = true;
 
    public function foo(true $v): true { /* ... */ *}
}

Compile time redundancy checks

As it is the case currently for false a compile time error will be emitted if true and bool are present in union. For example, the following function signature:

<?php
function test(): bool|true {}
?>

Will emit the following error:

Fatal error: Duplicate type true is redundant in %s on line %d

A compile time error is also emitted when true|false is used instead of bool. The following function signature:

<?php
function test(): false|true {}
?>

will emit the following error:

Fatal error: Type contains both true and false, bool should be used instead in %s on line %d

Examples

There are many examples of functions which only return true. Example of internal functions are all the array_sort functions always return true. And examples of userland projects which can benefit of this change is Composer, which has various cases where they could use a true type. [1] [2] [3] [4]

Reflection

The new true type will behave as any other type in regards to Reflection.

Backward Incompatible Changes

This RFC does not contain any backwards incompatible changes.

Proposed PHP Version

Next minor version, i.e. PHP 8.2.

Proposed Voting Choices

As per the voting RFC a yes/no vote with a 2/3 majority is needed for this proposal to be accepted.

Voting started on 2022-03-12 and will end on 2022-03-26.

Accept Add true type RFC?
Real name Yes No
aaronjunker (aaronjunker)  
alec (alec)  
asgrim (asgrim)  
ashnazg (ashnazg)  
brzuchal (brzuchal)  
crell (crell)  
dams (dams)  
danack (danack)  
derick (derick)  
dharman (dharman)  
ekin (ekin)  
galvao (galvao)  
girgias (girgias)  
kalle (kalle)  
kocsismate (kocsismate)  
lbarnaud (lbarnaud)  
levim (levim)  
lufei (lufei)  
marandall (marandall)  
mcmic (mcmic)  
nicolasgrekas (nicolasgrekas)  
ocramius (ocramius)  
petk (petk)  
pierrick (pierrick)  
ramsey (ramsey)  
reywob (reywob)  
sebastian (sebastian)  
seld (seld)  
sergey (sergey)  
theodorejb (theodorejb)  
trowski (trowski)  
villfa (villfa)  
wyrihaximus (wyrihaximus)  
Final result: 33 0
This poll has been closed.

Implementation

GitHub pull request: https://github.com/php/php-src/pull/XXXX

After the project is implemented, this section should contain

  • the version(s) it was merged into
  • a link to the git commit(s)
  • a link to the PHP manual entry for the feature

References

rfc/true-type.1649434706.txt.gz · Last modified: 2022/04/08 16:18 by girgias