Google 網路論壇不再支援新的 Usenet 貼文或訂閱項目,但過往內容仍可供查看。

Runtime Role Issues

瀏覽次數:7 次
跳到第一則未讀訊息

Ovid

未讀,
2006年10月10日 下午4:31:592006/10/10
收件者: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

未讀,
2006年10月11日 下午5:43:042006/10/11
收件者: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

未讀,
2006年10月11日 晚上8:11:292006/10/11
收件者: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

未讀,
2006年10月12日 凌晨2:39:252006/10/12
收件者: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

未讀,
2006年10月12日 凌晨4:10:132006/10/12
收件者: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

0 則新訊息