Runtime Role Issues

1 view
Skip to first unread message

Ovid

unread,
Oct 10, 2006, 4:31:59 PM10/10/06
to perl6...@perl.org
Hi all,

In doing a bit of work with traits (roles) in Perl 5
(http://perlmonks.org/?node_id=577477), I've realized some edge cases
which could be problematic.

First, when a role is applied to a class at runtime, a instance of that
class in another scope may specifically *not* want that role. Is there
a way of restricting a role to a particular lexical scope short of
merely applying that role to instances instead of classes?

Second, how can I remove roles from classes? I've create some code
which adds an "is_selected" method to some classes but when I'm done,
I'd like top easily remove that role. How do I do that? Seems closely
related to my first question, but it's still a distinct issue, I think.

Third, http://dev.perl.org/perl6/doc/design/syn/S12.html says:

You can also mixin a precomposed set of roles:

$fido does Sentry | Tricks | TailChasing | Scratch;

Should that be the following?

$fido does Sentry & Tricks & TailChasing & Scratch;

Cheers,
Ovid

--

Buy the book -- http://www.oreilly.com/catalog/perlhks/
Perl and CGI -- http://users.easystreet.com/ovid/cgi_course/

Tim Bunce

unread,
Oct 11, 2006, 5:43:04 PM10/11/06
to Ovid, perl6...@perl.org
On Tue, Oct 10, 2006 at 01:31:59PM -0700, Ovid wrote:
> Hi all,
>
> In doing a bit of work with traits (roles) in Perl 5
> (http://perlmonks.org/?node_id=577477), I've realized some edge cases
> which could be problematic.
>
> First, when a role is applied to a class at runtime, a instance of that
> class in another scope may specifically *not* want that role.

I always thought when a role is applied to a class at runtime you
get a new (anonymous) subclass. The original class isn't affected.

Tim.

Audrey Tang

unread,
Oct 11, 2006, 8:11:29 PM10/11/06
to Tim Bunce, Ovid, perl6...@perl.org

在 Oct 12, 2006 5:43 AM 時,Tim Bunce 寫到:

> On Tue, Oct 10, 2006 at 01:31:59PM -0700, Ovid wrote:
>> Hi all,
>>
>> In doing a bit of work with traits (roles) in Perl 5
>> (http://perlmonks.org/?node_id=577477), I've realized some edge cases
>> which could be problematic.
>>
>> First, when a role is applied to a class at runtime, a instance of
>> that
>> class in another scope may specifically *not* want that role.
>
> I always thought when a role is applied to a class at runtime you
> get a new (anonymous) subclass. The original class isn't affected.

Right, that's what usually happens with:

my Dog $fido .= new;
$fido does Catlike;

Here $fido's class become a new (memoized, anonymous) class that
still has
"Dog" as its name, but is no longer equivalent to the vanilla ^Dog
class object.

To forcefully add a role to a class at a distance during runtime, use
a class object call
(see Moose::Meta::Class for more about these APIs):

^Dog.add_role(^Catlike);

Thanks,
Audrey

Ovid

unread,
Oct 12, 2006, 2:39:25 AM10/12/06
to perl6...@perl.org
--- Audrey Tang <aud...@audreyt.org> wrote:

> > I always thought when a role is applied to a class at runtime you
> > get a new (anonymous) subclass. The original class isn't affected.
>
> Right, that's what usually happens with:
>
> my Dog $fido .= new;
> $fido does Catlike;

Am I missing something here? That's applying a role to an instance of
a class, not the class itself, and thus *should* create a new anonymous
class.



> To forcefully add a role to a class at a distance during runtime, use

> a class object call (see Moose::Meta::Class for more about these
> APIs):
>
> ^Dog.add_role(^Catlike);

That's more of what I was thinking, but where is this documented? I
can't find it in http://dev.perl.org/perl6/doc/design/syn/S12.html. I
see it in the Moose docs, but is this in Perl 6? I thought it was.

Audrey Tang

unread,
Oct 12, 2006, 4:10:13 AM10/12/06
to publiustemp...@yahoo.com, perl6...@perl.org

在 Oct 12, 2006 2:39 PM 時,Ovid 寫到:

> --To forcefully add a role to a class at a distance during runtime,

> use
>
>> a class object call (see Moose::Meta::Class for more about these
>> APIs):
>>
>> ^Dog.add_role(^Catlike);
>
> That's more of what I was thinking, but where is this documented? I
> can't find it in http://dev.perl.org/perl6/doc/design/syn/S12.html. I
> see it in the Moose docs, but is this in Perl 6? I thought it was.

Well, it's in Perl 6, and Moose is currently the metaobject layer
when the
VM that runs Perl 6 happens to be perl5...

Right after the 6.2.13 release (which is ready now, except for
changelogging),
we'll switch to the Moose team's MO system, ensuring that the
introspectio
APIs work exactly the same way on both GHC and perl5 virtual machines.

Thanks,
Audrey

Reply all
Reply to author
Forward
0 new messages