rfc:return_types

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:return_types [2015/01/14 00:33] – void in Java and C levimrfc:return_types [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 4: Line 4:
   * Date: 2014-03-20    * Date: 2014-03-20 
   * Author: Levi Morrison <levim@php.net>   * Author: Levi Morrison <levim@php.net>
-  * Status: Under Discussion+  * Status: Implemented (PHP 7.0)
   * First Published at: https://wiki.php.net/rfc/returntypehinting   * First Published at: https://wiki.php.net/rfc/returntypehinting
 +  * Migrated to: https://wiki.php.net/rfc/return_types
  
 ===== Introduction ===== ===== Introduction =====
Line 12: Line 13:
 Declaring return types has several motivators and use-cases: Declaring return types has several motivators and use-cases:
   * Prevent sub-types from breaking the expected return type of the super-type((See [[#variance_and_signature_validation|Variance and Signature Validation]] and [[#examples]] for more details on how this works)), especially in interfaces   * Prevent sub-types from breaking the expected return type of the super-type((See [[#variance_and_signature_validation|Variance and Signature Validation]] and [[#examples]] for more details on how this works)), especially in interfaces
-  * Prevent unintended return types+  * Prevent unintended return values
   * Document return type information in a way that is not easily invalidated (unlike comments)   * Document return type information in a way that is not easily invalidated (unlike comments)
  
 ===== Proposal ===== ===== Proposal =====
-This proposal adds an optional return type declaration to function declarations including closures, functions, generators, interface method declarations and class declarations. This RFC does not change the existing type declarations nor does it add new ones (see [[#differences_from_past_rfcs|differences from past RFCs]]).+This proposal adds an optional return type declaration to function declarations including closures, functions, generators,  and methods. This RFC does not change the existing type declarations nor does it add new ones (see [[#differences_from_past_rfcs|differences from past RFCs]]).
  
 Here is a brief example of the syntax in action: Here is a brief example of the syntax in action:
Line 102: Line 103:
   - Allowing ''null'' by default works against the purpose of type declarations. Type declarations make it easier to reason about the surrounding code. If ''null'' was allowed the programmer would always have to worry about the ''null'' case.   - Allowing ''null'' by default works against the purpose of type declarations. Type declarations make it easier to reason about the surrounding code. If ''null'' was allowed the programmer would always have to worry about the ''null'' case.
  
-The [[rfc:nullable_typehints|Nullable Types RFC]] addresses this shortcoming and more.+The [[rfc:nullable_types|Nullable Types RFC]] addresses this shortcoming and more.
  
 ==== Methods which cannot declare return types ==== ==== Methods which cannot declare return types ====
Line 165: Line 166:
 get_config(); get_config();
 </PHP> </PHP>
-''Catchable fatal error: The function get_config was expected to return an array and returned an integer in %s on line %d''+''Catchable fatal error: Return value of get_config() must be of the type arrayinteger returned in %s on line %d''
  
 ---- ----
Line 177: Line 178:
 answer(); answer();
 </PHP> </PHP>
-''Catchable fatal error: The function answer was expected to return an object of class int and returned an integer in %s on line %d''+''Catchable fatal error: Return value of answer() must be an instance of intinteger returned in %s on line %d''
  
 ---- ----
Line 189: Line 190:
 foo(); foo();
 </PHP> </PHP>
-''Catchable fatal error: The function foo was expected to return an object of class DateTime and returned null in %s on line %d''+''Catchable fatal error: Return value of foo() must be an instance of DateTimenull returned in %s on line %d''
  
 ---- ----
Line 209: Line 210:
 } }
 </PHP> </PHP>
-''Fatal error: Declaration of UserGateway_MySql::find should be compatible with UserGateway::find($id): User, return type missing in %s on line %d''+''Fatal error: Declaration of UserGateway_MySql::find() must be compatible with UserGateway::find($id): User in %s on line %d''
  
 ---- ----
Line 226: Line 227:
 This proposal specifically does not allow declaring multiple return types; this is out of the scope of this RFC and would require a separate RFC if desired. This proposal specifically does not allow declaring multiple return types; this is out of the scope of this RFC and would require a separate RFC if desired.
  
-If you want to use multiple return types, simply omit a return type declaration and rely on PHP's excellent dynamic nature.+If you want to use multiple return types in the meantime, simply omit a return type declaration and rely on PHP's excellent dynamic nature.
  
 ==== Reflection ==== ==== Reflection ====
Line 261: Line 262:
 This RFC modifies the PHP language syntax and therefore requires a two-third majority of votes. This RFC modifies the PHP language syntax and therefore requires a two-third majority of votes.
  
-<del>Should return types as outlined in this RFC be added to the PHP language? Voting will end on November 14th2014.</del> +Should return types as outlined in this RFC be added to the PHP language? Voting will end on January 232015
-A bug was found during the voting period that will require enough changes to how the RFC works that voting has been cancelled+<doodle title="Typed Returns" auth="levim" voteType="single" closed="true">
-<doodle title="Return Types" auth="levim" voteType="single" closed="true">+
    * Yes    * Yes
    * No    * No
Line 271: Line 271:
  
 Dmitry and I have updated the implementation to a more current master branch here: https://github.com/php/php-src/pull/997 Dmitry and I have updated the implementation to a more current master branch here: https://github.com/php/php-src/pull/997
 +
 +This RFC was merged into the master branch (PHP 7) in commit [[https://git.php.net/?p=php-src.git;a=commit;h=638d0cb7531525201e00577d5a77f1da3f84811e|638d0cb7531525201e00577d5a77f1da3f84811e]].
  
 ===== Future Work ===== ===== Future Work =====
 Ideas for future work which are out of the scope of this RFC include: Ideas for future work which are out of the scope of this RFC include:
  
-  * Allowing functions to declare that they do not return anything at all (''void'' in Java and C) +  * Allow functions to declare that they do not return anything at all (''void'' in Java and C) 
-  * Allowing nullable types (such as <php>?DateTime</php>). This is discussed in a related RFC: [[rfc:nullable_typehints|Declaring Nullable Types]] +  * Allow nullable types (such as <php>?DateTime</php>). This is discussed in the [[rfc:nullable_types|Nullable Types]] RFC. 
-  * Improving parameter variance. Currently parameter types are invariant while they could be contravariant. +  * Improve parameter variance. Currently parameter types are invariant while they could be contravariant. Change the E_STRICT on mismatching parameter types to E_COMPILE_ERROR
-  * Improving runtime performance by doing type analysis. +  * Improve runtime performance by doing type analysis. 
-  * Updating documentation to use the new return type syntax.+  * Update documentation to use the new return type syntax.
  
 ===== References ===== ===== References =====
rfc/return_types.1421195609.txt.gz · Last modified: 2017/09/22 13:28 (external edit)