rfc:null-standalone-type

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
rfc:null-standalone-type [2021/10/02 14:55] – created girgiasrfc:null-standalone-type [2022/02/20 17:51] (current) – Withdraw this RFC in favour of null + false standalone RFC girgias
Line 1: Line 1:
 ====== PHP RFC: Allow null as standalone type ====== ====== PHP RFC: Allow null as standalone type ======
  
-  * Version: 0.1+  * Version: 0.2
   * Date: 2021-10-02   * Date: 2021-10-02
   * Author: George Peter Banyard, <girgias@php.net>   * Author: George Peter Banyard, <girgias@php.net>
-  * Status: Under Discussion+  * Status: Withdrawn
   * Target Version: PHP 8.2   * Target Version: PHP 8.2
-  * Implementation: [[https://github.com/php/php-src/pull/xxxx]]+  * Implementation: [[https://github.com/php/php-src/pull/7546]]
   * First Published at: [[http://wiki.php.net/rfc/null-standalone-type]]   * First Published at: [[http://wiki.php.net/rfc/null-standalone-type]]
 +  * Superseded by: [[https://wiki.php.net/rfc/null-false-standalone-types]]
  
 ===== Introduction ===== ===== Introduction =====
Line 42: Line 43:
  
 A method, from a built-in PHP class, which could benefit from declaring its return value as ''null'' is <php>SplFileObject::getChildren()</php> A method, from a built-in PHP class, which could benefit from declaring its return value as ''null'' is <php>SplFileObject::getChildren()</php>
 +
 +
 +==== Distinction between null and void ====
 +
 +A function always returns a value in PHP, even if its return type is declared as ''void'' where ''NULL''
 +is the value returned. The union type RFC did not include support for ''null'' for the following reason:
 +
 +<blockquote>
 +The null type is only allowed as part of a union, and can not be used as a standalone type. Allowing it as a standalone type would make both <php>function foo(): void</php> and <php>function foo(): null</php> legal function signatures, with similar but not identical semantics. This would negatively impact teachability for an unclear benefit.
 +</blockquote>
 +
 +As explained previously, there are clear reasons as to why one may need to use ''null'' as a return type,
 +as ''void'' is not a subtype of any other type and lives on its own in the type hirarchy.
 +
 +Moreover, a function which has a ''void'' return type must only use ''return;'' wherease one with ''null'' must use ''return null;''.
  
 ===== Proposal ===== ===== Proposal =====
Line 54: Line 70:
  
 </code> </code>
 +
 ==== Non-support for standalone-like usage of false ==== ==== Non-support for standalone-like usage of false ====
  
Line 60: Line 77:
 ==== Redundancy of ?null ==== ==== Redundancy of ?null ====
  
-Trying to mark ''null'' as nullable will result in a compile time error.+Trying to mark ''null'' as nullable will result in a compile time error
 +in line with PHP's current type resolving redundancy rules.
  
 ==== Reflection ==== ==== Reflection ====
Line 78: Line 96:
 As per the voting RFC a yes/no vote with a 2/3 majority is needed for this proposal to be accepted. As per the voting RFC a yes/no vote with a 2/3 majority is needed for this proposal to be accepted.
  
-===== Patches and Tests ===== +===== Implementation =====
- +
-Links to any external patches and tests go here.+
  
-If there is no patch, make it clear who will create a patch, or whether a volunteer to help with implementation is needed. +GitHub pull request: https://github.com/php/php-src/pull/7546
- +
-Make it clear if the patch is intended to be the final patch, or is just a prototype. +
- +
-For changes affecting the core language, you should also provide a patch for the language specification. +
- +
-===== Implementation =====+
  
 After the project is implemented, this section should contain After the project is implemented, this section should contain
rfc/null-standalone-type.1633186556.txt.gz · Last modified: 2021/10/02 14:55 by girgias