rfc:mixed-typehint

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
rfc:mixed-typehint [2017/12/19 03:03] – status update majklrfc:mixed-typehint [2018/06/29 13:35] – clarifications, added mixed vs. void section majkl
Line 1: Line 1:
 ====== PHP RFC: Mixed typehint ====== ====== PHP RFC: Mixed typehint ======
-  * Version: 1.0+  * Version: 1.1
   * Date: 2017-07-19   * Date: 2017-07-19
   * Author: Michael Moravec (php.net@majkl578.cz)   * Author: Michael Moravec (php.net@majkl578.cz)
Line 10: Line 10:
  
 ===== Proposal ===== ===== Proposal =====
 +
 This RFC proposes to add the ''mixed'' type to be used for parameter and return types to explicitly declare desired type, instead of being forced to not declare anything. This RFC proposes to add the ''mixed'' type to be used for parameter and return types to explicitly declare desired type, instead of being forced to not declare anything.
  
Line 18: Line 19:
  
 ===== Nullability ===== ===== Nullability =====
-As the ''mixed'' type accepts //anything//, including null, nullable mixed type (''?mixed'') is disabled at compile time.+ 
 +As the ''mixed'' is a //union type// that accepts //any type//, including null, nullable mixed type (''?mixed'') is disabled at compile time as ''null'' is implied.
  
 <code php> <code php>
-function foo(): ?mixed {}+function foo() : ?mixed {}
 // Fatal error // Fatal error
 </code> </code>
Line 29: Line 31:
 No changes to variance needed. Exactly same rules as when no type is present apply. No changes to variance needed. Exactly same rules as when no type is present apply.
  
-Since the ''mixed'' type and no type are functionally equivalent, the following code is functionally equivalent as well:+Since the ''mixed'' type and no type are functionally (not semantically) equivalent, the following code is functionally (but not semantically) equivalent as well:
 <code php> <code php>
 function foo($arg) { function foo($arg) {
Line 42: Line 44:
 </code> </code>
  
-Same applies to inheritance, where mixed type and no type could be interchanged without any error. This code would work just fine:+Same rules apply to inheritance, where mixed type and no type could be interchanged without any error. This code would work just fine:
 <code php> <code php>
 class Foo { class Foo {
Line 60: Line 62:
 } }
 </code> </code>
 +
 +===== Mixed vs. Void =====
 +
 +As of PHP 7.1, PHP has a special ''void'' type. ''Void'' is not a regular type and is only valid for return types to specify that //nothing is returned//.
 +
 +In some other languages like C/C++ ''void'' can also be used as a //type// that accepts //any// type.
 +This essentially matches the behavior of ''mixed'' in PHP.
 +
 +As void is not a real type in PHP, it is not part of ''mixed'' type.
 +Should there ever be a type that includes all types //and// ''void'', it would be supertype of ''mixed'' and ''void'', i.e. ''any''.
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
 +
 None, ''mixed'' is already reserved word since PHP 7.0. None, ''mixed'' is already reserved word since PHP 7.0.
 +Variance behavior is same as when no type is specified.
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
 +
 7.3 7.3
  
 ===== RFC Impact ===== ===== RFC Impact =====
 ==== To SAPIs ==== ==== To SAPIs ====
 +
 None. None.
  
 ==== To Existing Extensions ==== ==== To Existing Extensions ====
 +
 None. None.
  
 ==== To Opcache ==== ==== To Opcache ====
-Not analyzed.+ 
 +Not analyzed, likely none (no changes to current language behavior).
  
 ===== 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://github.com/php/php-src/pull/2603|GitHub Pull request #2063]] [[https://github.com/php/php-src/pull/2603|GitHub Pull request #2063]]
  
rfc/mixed-typehint.txt · Last modified: 2020/07/22 09:03 by kocsismate