rfc:returntypehinting
Differences
This shows you the differences between two versions of the page.
rfc:returntypehinting [2015/01/13 03:04] – Grammar levim | rfc:returntypehinting [2017/09/22 13:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | + | This RFC has moved to [[https:// | |
- | ====== PHP RFC: Return Type Declarations ====== | + | |
- | * Version: 1.4 | + | |
- | * Date: 2014-03-20 | + | |
- | * Author: Levi Morrison < | + | |
- | * Status: Under Discussion | + | |
- | * First Published at: https:// | + | |
- | + | ||
- | ===== Introduction ===== | + | |
- | Many developers would like to be able to declare the return type of a function. The basic idea of declaring a return type has been included in at least three RFCs and has been discussed in a few other places (see [[# | + | |
- | + | ||
- | Declaring return types has several motivators and use-cases: | + | |
- | * Providing return types on interface implementations((See [[# | + | |
- | * Preventing sub-classes | + | |
- | * Preventing unintended return types | + | |
- | * Documenting return type information that is not easily invalidated (unlike comments). | + | |
- | + | ||
- | ===== 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 [[# | + | |
- | + | ||
- | Example of '' | + | |
- | + | ||
- | " | + | |
- | inner_statement_list | + | |
- | " | + | |
- | + | ||
- | Here is a brief example of the syntax in action: | + | |
- | < | + | |
- | function foo(): array { | + | |
- | return []; | + | |
- | } | + | |
- | </ | + | |
- | More examples can be found in the [[# | + | |
- | + | ||
- | //Code which does not declare a return type will | + | |
- | | + | |
- | + | ||
- | ==== Variance and Signature Validation ==== | + | |
- | The enforcement of the declared return type during inheritance is invariant; this means that when a sub-type overrides a parent method then the return type of the child must exactly match the parent and may not be omitted. If the parent does not declare a return type then the child is allowed to declare one. | + | |
- | + | ||
- | If a mismatch is detected during compile time (e.g. a class improperly overriding a return type) then '' | + | |
- | + | ||
- | Covariant return types are considered to be type sound and are used in many other languages((C++, | + | |
- | + | ||
- | Note that this topic of variance is about the declared return type of the function; this means that the following would be valid for either invariant or covariant return types: | + | |
- | + | ||
- | < | + | |
- | interface A { | + | |
- | static function make(): A; | + | |
- | } | + | |
- | class B implements A { | + | |
- | static function make(): A { | + | |
- | return new B(); | + | |
- | } | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | The class '' | + | |
- | + | ||
- | < | + | |
- | interface A { | + | |
- | static function make(): A; | + | |
- | } | + | |
- | class B implements A { | + | |
- | static function make(): B { // must exactly match parent; this will error | + | |
- | return new B(); | + | |
- | } | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | The above sample does not work because this RFC proposes only invariant return types; this could be extended in the future to be allowed. | + | |
- | + | ||
- | ==== Position of Type Declaration ==== | + | |
- | The two major conventions in other programming languages for placing return type information are: | + | |
- | + | ||
- | * Before the function name | + | |
- | * After the parameter list's closing parenthesis | + | |
- | + | ||
- | The former position has been proposed in the past and the RFCs were either declined or withdrawn. One cited issue is that many developers wanted to preserve the ability to search for < | + | |
- | + | ||
- | The latter position is used in several languages(([[http:// | + | |
- | + | ||
- | Declaring the return type after the parameter list had no shift/ | + | |
- | + | ||
- | ==== Returning by Reference ==== | + | |
- | + | ||
- | This RFC does not change the location of ''&'' | + | |
- | < | + | |
- | function & | + | |
- | return $data; | + | |
- | } | + | |
- | + | ||
- | function & | + | |
- | return $data; | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | ==== Disallowing NULL on Return Types ==== | + | |
- | Consider the following function: | + | |
- | + | ||
- | < | + | |
- | function foo(): DateTime { | + | |
- | return null; // invalid | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | It declares that it will return '' | + | |
- | + | ||
- | - This aligns with current parameter type behavior. When parameters have a type declared, a value of '' | + | |
- | - Allowing '' | + | |
- | + | ||
- | The [[rfc: | + | |
- | + | ||
- | ==== Methods which cannot declare return types ==== | + | |
- | + | ||
- | Class constructors, | + | |
- | + | ||
- | * ''< | + | |
- | * ''< | + | |
- | * ''< | + | |
- | + | ||
- | ==== Examples ==== | + | |
- | Here are some snippets of both valid and invalid usage. | + | |
- | + | ||
- | === Examples of Valid Use === | + | |
- | < | + | |
- | // Overriding a method that did not have a return type: | + | |
- | interface Comment {} | + | |
- | interface CommentsIterator extends Iterator { | + | |
- | function current(): Comment; | + | |
- | } | + | |
- | </ | + | |
- | < | + | |
- | // Using a generator: | + | |
- | + | ||
- | interface Collection extends IteratorAggregate { | + | |
- | function getIterator(): | + | |
- | } | + | |
- | + | ||
- | class SomeCollection implements Collection { | + | |
- | function getIterator(): | + | |
- | foreach ($this-> | + | |
- | yield $key => $value; | + | |
- | } | + | |
- | } | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | === Examples of Invalid Use === | + | |
- | + | ||
- | The error messages are taken from the current patch. | + | |
- | ---- | + | |
- | + | ||
- | < | + | |
- | // Covariant return-type: | + | |
- | + | ||
- | interface Collection { | + | |
- | function map(callable $fn): Collection; | + | |
- | } | + | |
- | + | ||
- | interface Set extends Collection { | + | |
- | function map(callable $fn): Set; | + | |
- | } | + | |
- | </ | + | |
- | '' | + | |
- | ---- | + | |
- | < | + | |
- | // Returned type does not match the type declaration | + | |
- | + | ||
- | function get_config(): | + | |
- | return 42; | + | |
- | } | + | |
- | get_config(); | + | |
- | </ | + | |
- | '' | + | |
- | + | ||
- | ---- | + | |
- | + | ||
- | < | + | |
- | // Int is not a valid type declaration | + | |
- | + | ||
- | function answer(): int { | + | |
- | return 42; | + | |
- | } | + | |
- | answer(); | + | |
- | </ | + | |
- | '' | + | |
- | + | ||
- | ---- | + | |
- | + | ||
- | < | + | |
- | // Cannot return null with a return type declaration | + | |
- | + | ||
- | function foo(): DateTime { | + | |
- | return null; | + | |
- | } | + | |
- | foo(); | + | |
- | </ | + | |
- | '' | + | |
- | + | ||
- | ---- | + | |
- | + | ||
- | < | + | |
- | // Missing return type on override | + | |
- | + | ||
- | class User {} | + | |
- | + | ||
- | interface UserGateway { | + | |
- | function find($id): User; | + | |
- | } | + | |
- | + | ||
- | class UserGateway_MySql implements UserGateway { | + | |
- | // must return User or subtype of User | + | |
- | function find($id) { | + | |
- | return new User(); | + | |
- | } | + | |
- | } | + | |
- | </ | + | |
- | '' | + | |
- | + | ||
- | ---- | + | |
- | + | ||
- | < | + | |
- | // Generator return types can only be declared as Generator, Iterator or Traversable (compile time check) | + | |
- | + | ||
- | function foo(): array { | + | |
- | yield []; | + | |
- | } | + | |
- | </ | + | |
- | '' | + | |
- | + | ||
- | + | ||
- | ==== Multiple Return Types ==== | + | |
- | 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. | + | |
- | + | ||
- | ==== Reflection ==== | + | |
- | + | ||
- | This RFC purposefully omits reflection support as there is an open RFC about improving type information in reflection: | + | |
- | + | ||
- | ==== Differences from Past RFCs ==== | + | |
- | This proposal differs from past RFCs in several key ways: | + | |
- | + | ||
- | * **The return type is positioned after the parameter list.** See [[# | + | |
- | * **We keep the current type options.** Past proposals have suggested new types such as '' | + | |
- | * **We keep the current search patterns.** You can still search for < | + | |
- | * **We allow return type declarations on all function types**. Will Fitch' | + | |
- | * **We do not modify or add keywords.** Past RFCs have proposed new keywords such as '' | + | |
- | + | ||
- | ===== Other Impact ===== | + | |
- | + | ||
- | ==== On Backward Compatiblity ==== | + | |
- | This RFC is backwards compatible with previous PHP releases. | + | |
- | + | ||
- | ==== On SAPIs ==== | + | |
- | There is no impact on any SAPI. | + | |
- | + | ||
- | ==== On Existing Extensions ===== | + | |
- | The structs '' | + | |
- | + | ||
- | ==== On Performance ==== | + | |
- | An informal test indicates that performance has not seriously degraded. More formal performance testing can be done before voting phase. | + | |
- | + | ||
- | ===== Proposed PHP Version(s) ===== | + | |
- | This RFC targets PHP 7. | + | |
- | + | ||
- | ===== Vote ===== | + | |
- | This RFC modifies the PHP language syntax and therefore requires a two-third majority of votes. | + | |
- | + | ||
- | < | + | |
- | 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=" | + | |
- | * Yes | + | |
- | * No | + | |
- | </ | + | |
- | + | ||
- | ===== Patches and Tests ===== | + | |
- | + | ||
- | Dmitry has updated the implementation to a more current master branch here: https:// | + | |
- | + | ||
- | ===== Future Work ===== | + | |
- | 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 ('' | + | |
- | * Allowing nullable types (such as < | + | |
- | * Improving parameter variance. Currently parameter types are invariant while they could be contravariant. | + | |
- | * Improving runtime performance by doing type analysis. | + | |
- | * Updating documentation to use the new return type syntax. | + | |
- | + | ||
- | ===== References ===== | + | |
- | * [[rfc: | + | |
- | * [[rfc: | + | |
- | * [[rfc: | + | |
- | * [[http:// | + | |
- | + | ||
- | In the meeting in Paris on November 2005 it was decided that PHP should have return type declarations and some suggestions were made for syntax. Suggestion 5 is nearly compatible with this RFC; however, it requires the addition of a new token '' | + | |
- | + | ||
- | The following (tiny) patch would allow the syntax in suggestion 5 to be used alongside the current syntax. This RFC does not propose that both versions of syntax should be used; the patch just shows how similar this RFC is to that suggestion from 2005. | + | |
- | + | ||
- | https:// | + | |
- | + | ||
- | ===== Changelog ===== | + | |
- | + | ||
- | * v1.1: RFC now targets PHP 7. | + | |
- | * v1.2: Disallow return types for constructors, | + | |
- | * v1.3: Reworked Reflection support to use new '' | + | |
- | * v1.3.1: Renamed '' | + | |
- | * v1.4: Change to invariant return types and omit reflection support | + |
rfc/returntypehinting.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1