Skip Menu |

This queue is for tickets about the Class-Trait CPAN distribution.

Report information
The Basics
Id: 27501
Status: rejected
Priority: 0/
Queue: Class-Trait

People
Owner: Nobody in particular
Requestors: mschwern [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: (no value)



Subject: What does SUPER do?
Its not clear what using SUPER inside a trait's method will do. Looking at the tests it seems it will DWIM, it will start looking in the class which used the trait, but explicit docs would be nice. Probably also worth mentioning interactions with NEXT.
On Fri Jun 08 11:50:14 2007, MSCHWERN wrote: Show quoted text
> Its not clear what using SUPER inside a trait's method will do. Looking > at the tests it seems it will DWIM, it will start looking in the class > which used the trait, but explicit docs would be nice.
Oh, it doesn't DWIM. I remember discussing this last you were in PDX. Because the trait injects its code directly into the using class there's no way for the trait user to define its own method. Therefore this will not DWIM: package TTest; use Class::Trait 'base'; our @REQUIRES = qw(name); sub name { my $self = shift; return "Name: ". $self->SUPER::name; } package Foo; use Class::Trait "TTest"; sub new { bless {} } sub name { "wibble" } package main; my $foo = Foo->new; print $foo->name; The use-case here is I have a set of ORDM functionality having to do with transactions. Some of this has to override existing methods, for example, update(). The trait user also might want to override the same methods with their own extra stuff. Both the trait's update() and the trait user's update() should run. SUPER does not have to be the answer. Something like NEXT might also work. But in order for it to work at all there has to be a way for the trait and the trait user to have the same method defined.
This is documented (though not as well as it could be). The behavior of traits is specified very clearly in this regard. Both the original traits paper and the Class::Trait module agree on this: package TTest; use Class::Trait 'base'; our @REQUIRES = qw(name); sub name { my $self = shift; return "Name: ". $self->SUPER::name; } package Foo; use Class::Trait "TTest"; sub new { bless {} } sub name { "wibble" } package main; my $foo = Foo->new; print $foo->name; That should *not* compile. A trait must not be compiled into a class with a method of the same name. It's strictly forbidden. You must either alias the trait or apply() it at runtime (which creates an anonymous subclass). use Class::Trait 'TTest' => { alias => { name => 'trait_name' } }; Or: Class::Trait->apply( 'Foo' => 'TTest' ); Cheers, Ovid