Both sides previous revisionPrevious revisionNext revision | Previous revision |
rfc:allow-void-variance [2019/02/04 04:05] – wesnetmo | rfc:allow-void-variance [2019/02/04 07:48] (current) – wesnetmo |
---|
* First Published at: http://wiki.php.net/rfc/allow-void-variance | * First Published at: http://wiki.php.net/rfc/allow-void-variance |
| |
===== Rationale ===== | ===== Introduction ===== |
| |
This RFC proposes to change ''void'' 's behavior in class methods, because the current behavior resulted to cause more annoyance than benefit. What is proposed is backward compatible and in accordance with the goals of the previous ''void'' RFC. | This RFC proposes to change in part ''void'' 's behavior in class methods; the current behavior is causing more annoyance than benefit and is probably too restrictive regardless. What is proposed here is backward-compatible and in accordance with the goals of the previous ''void'' RFC. |
| |
| ===== Rationale ===== |
| |
The intent of ''void'' should be simply making clear to the reader that the function does not return anything useful, and that any attempt to use the returned value should be considered an invalid operation. | **The intent of ''void'' should be simply making clear to the reader that the function does not return anything useful, and that any attempt to use the returned value should be considered an invalid operation.** |
| |
It shouldn't do more than that, but currently it also denies methods to be overridden with different types in subclasses. | It shouldn't do more than that, but currently it also denies methods to be overridden with different types in subclasses. |
function myFooConsumer (Foo $foo) { | function myFooConsumer (Foo $foo) { |
| |
// method() is void, therefore we won't be using its return type | // method() is void, therefore we won't be using its return value |
| |
$foo->method(); | $foo->method(); |
The ''method()'' can return anything, or nothing at all, but ''myFooConsumer'' just does not care about it. | The ''method()'' can return anything, or nothing at all, but ''myFooConsumer'' just does not care about it. |
| |
For this reason adding a type to ''void'' in subclasses is not an invalid operation, and denying it is a pointless restriction. Changing from ''void'' to something else is very probably a bad idea, but PHP should not enforce ''void'' in subclasses just on that basis. | For this reason adding a type to ''void'' in subclasses is not an invalid operation, and denying it is a pointless restriction. Changing from ''void'' to something else is probably a bad idea, but PHP should not enforce ''void'' in subclasses just on that basis. |
| |
===== Proposal ===== | ===== Proposal ===== |
} | } |
</code> | </code> |
| |
| ===== Precedents in other languages ===== |
| |
| TypeScript does this (are there other languages?). |
| |
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== |