As it turns outs, implementing traits at runtime isn't too hard. It's the syntax which is killing me. Let's say that I have a new "Dog" object and I want my dog to do tricks. In Perl 6:
$fido does Tricks;
Class::Trait was not originally designed to support runtime trait composition so the does method either returns a list of traits a class can do or returns a boolean value indicating that a particular trait passed to it is avaiable. Thus, we use the apply method for adding traits. However, this raises a number of thorny issues. First, according to Synopsis 12, roles can be applied at runtime to both classes and instances and this will create a new anonymous class. For instances, this is easy. What does this mean for classes, though? I'm not sure. Bummer.
Applying them at runtime for instances is easier. I need to rebless an object into the appropriate anonymous class and flatten the trait methods into that class. However, I don't have an appropriate syntax. I could do this:
my $fido = Dog->new;
Unfortunately, Synopsis 12 makes it clear that we should be able to apply several traits to fido at once, flattening them into a new anonymous class. The only way I can see to make that work is a variant of this:
That seems like a really ugly syntax. However, more than this, how would I then apply traits to a class? I could do this:
That seems like an even uglier syntax but I can't think of a better way. For the time being, runtime trait application seems best suited for instances, so I'll focus on that.