An email I wrote to the Perl 6 language list.
(OK, the subject sucked, but I tried
You can, however, say
$fido does Sentry;
$fido does Tricks;
$fido does TailChasing;
$fido does Scratch;
Unlike the compile-time role composition, each of these layers on a new mixin with a new level of inheritance, creating a new anonymous class for dear old Fido, so that a
Recently I reported a bug in Moose's runtime role application. The bug was caused by repeated runtime role application causing recursive inheritance warnings. The problem was very hard to track down because I was effectively doing something like this:
$object->meta->apply('Some::Role') while 1;
(The reality is that we had a singleton with a role applied to it multiple times).
Eventually, the code broke and this was real fun to debug, but I can imagine a scenario for this being natural. Imagine that tour REST interface returns XML, but sometimes someone wants YAML. So you have:
$resultset does Role::Serializable::XML;
$resultset does Role::Serializable::YAML;
Since you cache resultsets if they've not changed, you could easily have the XML and YAML roles getting reapplied at runtime multiple times.
I don't see anything in the spec addressing this. Aside from "don't do that", is this something which can be addressed in perl instead of Perl? This problem is closely related to the "ordering problem" with mixins and multiple inheritance that roles were designed to avoid!
I think this issue could be mitigated by allowing runtime role application to be lexically scoped:
temp $resultset does Role::Serializable::YAML;
I think that would work, but I'm unsure.