Re: [RFC][Discuss] Covariant return- and contravariant parameter- types

From: Date: Tue, 27 Nov 2018 21:14:43 +0000
Subject: Re: [RFC][Discuss] Covariant return- and contravariant parameter- types
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-103523@lists.php.net to get a copy of this message
On Tue, Nov 27, 2018 at 2:00 PM Levi Morrison <levim@php.net> wrote: > > On Tue, Nov 27, 2018 at 11:36 AM Larry Garfield <larry@garfieldtech.com> wrote: > > > > On Monday, November 26, 2018 3:25:52 PM CST Levi Morrison wrote: > > > I am happy to announce my latest RFC, [Covariant Returns and > > > Contravariant Parameters][1], is open for discussion. If you do not > > > recognize those terms then hopefully the introduction coupled with the > > > [examples][2] will help. Here is one snippet that demonstrates > > > covariant return types: > > > > > > <?php > > > interface Repository { > > > function fetch(int $id): ?Entity; > > > } > > > > > > class UserFactory implements Repository { > > > function fetch(int $id): ?UserEntity { > > > /* Since UserEntity is an Entity, this is now allowed */ > > > /* ... implementation ... */ > > > } > > > } > > > ?> > > > > > > Covariant return types and contravariant parameter types are highly > > > requested features; here are a few of the bug reports: > > > > > > * https://bugs.php.net/bug.php?id=71825 > > > * https://bugs.php.net/bug.php?id=72442 > > > * https://bugs.php.net/bug.php?id=75385 > > > * https://bugs.php.net/bug.php?id=75823 > > > > > > There is a [preliminary implementation][3] that is available. It will > > > not be rebased during discussion to allow people to check out the code > > > and get updates without worrying about it breaking. The code quality > > > can definitely be improved, and there are still a few todo comments in > > > the code. > > > > > > Please do not discuss the items found in [Future Scope][4] as part of > > > this thread, which includes discussing types such as > > > mixed, > > > scalar, numeric, etc. > > > > > > I look forward to your feedback, and especially with your > > > experimentation with the implementation. > > > > > > Levi Morrison > > > > > > [1]: > > > > > > https://wiki.php.net/rfc/covariant-returns-and-contravariant-parameters > > > [2]: > > > > > > https://wiki.php.net/rfc/covariant-returns-and-contravariant-parameters#exa > > > mples [3]: > > > þÎ2àà¯j > > > �z6®f2|https://github.com/php/php-src/compare/master...morrisonlevi:variance2 [4]: > > > > > > https://wiki.php.net/rfc/covariant-returns-and-contravariant-parameters#fut > > > ure_scope > > > > Looks like fun! > > > > Would this also include allowing a return type of static, > > which would be > > useful for the "Return the object that was invoked" case or "return a new > > instance of the same object" case? Currently we're limited to > > return self, > > which is, of course, not the same thing and less flexible. > > > > --Larry Garfield > > It does not. To be honest, I did not think of it until your question. After thinking about it, I believe it is out of scope for this RFC. While static would be covariant, it's also more than that. Accordingly, it deserves to have its own consideration and voting.

« previous php.internals (#103523) next »