Runtime trait composition to the CPAN

Ovid on 2005-11-26T23:45:57

Soon Class::Trait 0.20 will be on the CPAN. We now have runtime trait composition and I'm just shy of 400 tests. The basic syntax works like this:

Class::Trait->apply($class_or_instance, @traits);

If you apply traits at runtime to a class, all instances (and subclasses) of that class get the new methods. If you apply traits to an instance, only that instance will get the new methods and the instance will be blessed into a new anonymous class.

In both cases, runtime trait application is similar to "mixins" whereby the trait methods will redefine (or override) any existing methods they find. When applying traits at compile time, existing methods take precedence and the trait's methods are silently discarded.

There are also some documentation updates, including a small section explaining when you would want to use traits.

There's a fair amount of ugly hackery to get Class::Trait->does to work and I have a bit of tramp data being passed around. Still, the test suite is fairly solid so I can revisit this later. Bug reports, of course, would be wonderful. Patches even more so.


Partial methods

djberg96 on 2006-01-03T14:00:21

I found this article on traits interesting.

It's the language Heron, mind you, but I thought the ability to create partially implemented required functions was interesting.

- Dan