On Thu, Feb 18, 2016 at 1:29 PM Nikita Popov <nikita....@gmail.com> wrote:
> On Wed, Feb 17, 2016 at 3:25 PM, Kevin Gessner <kgess...@etsy.com> wrote: > > > Hello internals team! I'd like to propose an RFC to allow traits to > > implement interfaces. > > > > I've noticed s pattern in Etsy's code and elsewhere, where a trait > provides > > a common implementation of an interface. Classes that use the trait are > > required to also explicitly declare the interface to benefit. I propose > > that traits be permitted to declare and implement interfaces. Classes > that > > use such a trait would then implement the interface, as though it were > > declared on the class, without declaring the interface explicitly. > > > > I believe this small change would be a useful improvement to the OO > > system. I've written a draft RFC, but I don't have karma to create the > > wiki page for it. Could someone please grant wiki karma to my account, > > kevingessner? > > > > I don't yet have an implementation, but I'll be starting on one once I've > > posted the RFC. I look forward to your thoughts and feedback. > > > > Without given an opinion on the RFC itself, this might be interesting for > context: > > http://hhvm.com/blog/9581/trait-and-interface-requirements-in-hack > > HHVM already supports "trait Foo implements Iface" with the semantics that > the interface is also implemented by the using class. > > Additionally HHVM supports a notion of "require implements Iface" which > means that the using class must implement this interface. > > Why should it matter if the class using the trait actually implements the interface? If the person just wants the functionality, but, for whatever reason, doesn't want to actually implement the interface, why should it matter? I think the trait should HELP the person implementing an interface. The trait should be able to say (whether via comments or an engine enforceable construct) "Hey, I've done everything needed for you to implement this interface" but, at no time, should the trait force the class utilizing it to interact (via extend or implement or use) a 3rd artifact. trait foo implements myinterface {} class bar { use foo;} class baz implements myinterface { use foo;} Both of the above should be legal, and $myBar instanceof myinterface should return false; Traits also already have the ability to force the using class to define certain functionality by declaring methods abstract - however, the trait doesn't care if it is implementing that method via inheritance, because some other interface requires it be implement, or via another trait. > Nikita > -- -- Chase chasepee...@gmail.com