Traits - does a trait trigger any callback when used on a class?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Traits - does a trait trigger any callback when used on a class?

Sophie424
I am interested in taking some action whenever a particular trait is used on
a class. Is there a hook available?

Thanks - Sophie




Reply | Threaded
Open this post in threaded view
|

Re: Traits - does a trait trigger any callback when used on a class?

Tom Phoenix
On Jan 8, 2008 2:20 AM, itsme213 <[hidden email]> wrote:

> I am interested in taking some action whenever a particular trait
> is used on a class. Is there a hook available?

When is a trait "used" on a class? I think it's used whenever a method
of that class is compiled. Do you want to change what the compiler is
doing?

Or do you mean that you want to do something when a trait is first
applied to a class?

What are you trying to accomplish? What problem are you trying to solve?

Cheers!

--Tom Phoenix

Reply | Threaded
Open this post in threaded view
|

Re: Traits - does a trait trigger any callback when used on a class?

Sophie424

"Tom Phoenix" <[hidden email]> wrote in message
> Or do you mean that you want to do something when a trait is first
> applied to a class?

First applied. I am looking for callback / hooks similar to what Ruby has so
I can do some extra processing.

Sophie




Reply | Threaded
Open this post in threaded view
|

Re: Traits - does a trait trigger any callback when used on a class?

Tom Phoenix
On Jan 8, 2008 1:16 PM, itsme213 <[hidden email]> wrote:
> "Tom Phoenix" <[hidden email]> wrote in message
> > Or do you mean that you want to do something when a trait is first
> > applied to a class?
>
> First applied.

You could install a hook somewhere around the time the class is first
instantiated, I suppose. But wouldn't that break some of the rules of
traits, if there's more processing going on than what shows in the
code of the trait itself?

Maybe what you want is to have your trait use a class trait that would
do some initialization?

Good luck with it!

--Tom Phoenix

Reply | Threaded
Open this post in threaded view
|

Re: Traits - does a trait trigger any callback when used on a class?

Jason Johnson-5
My understand was that a trait simply stuffs it's methods into the
class it is a part of, so afterwards it looks like one just
implemented the methods in the class as normal.

I'm sure there must be a way of catching entry and exit of a specific
method, though I'm not sure what you would have to do.  Possibly some
byte code injection like how Aspects work in Java.

On Jan 8, 2008 11:31 PM, Tom Phoenix <[hidden email]> wrote:

> On Jan 8, 2008 1:16 PM, itsme213 <[hidden email]> wrote:
> > "Tom Phoenix" <[hidden email]> wrote in message
> > > Or do you mean that you want to do something when a trait is first
> > > applied to a class?
> >
> > First applied.
>
> You could install a hook somewhere around the time the class is first
> instantiated, I suppose. But wouldn't that break some of the rules of
> traits, if there's more processing going on than what shows in the
> code of the trait itself?
>
> Maybe what you want is to have your trait use a class trait that would
> do some initialization?
>
> Good luck with it!
>
> --Tom Phoenix
>
>