rfc:allow-void-variance

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:allow-void-variance [2019/02/04 03:55] wesnetmorfc:allow-void-variance [2019/02/04 07:48] (current) wesnetmo
Line 6: Line 6:
   * 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 methodsthe 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. 
Line 33: Line 35:
 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();
Line 46: Line 48:
 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 subclasses is not an invalid operation, and denying it is a pointless restriction.+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 =====
Line 90: Line 92:
 </code> </code>
  
-This is not technically invalid since ''mixed'' includes ''null'', but it certainly feels "not LSP-valid" and it's therefore disallowed by this RFC.+This is technically not invalid since ''mixed'' includes ''null'', but it certainly feels "not LSP-valid" and it's therefore disallowed by this RFC
 + 
 +And, more obviously, also the following is disallowed: 
 + 
 +<code php> 
 +class Foo{ 
 +    function method (): void   {} 
 +
 + 
 +class Bar extends Foo{ 
 +    function method (): string {} // ok 
 +
 + 
 +class Baz extends Bar{ 
 +    function method (): void   {} // error: can't go back to void 
 +
 +</code> 
 + 
 +===== Precedents in other languages ===== 
 + 
 +TypeScript does this (are there other languages?).
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
rfc/allow-void-variance.1549252514.txt.gz · Last modified: 2019/02/04 03:55 by wesnetmo