Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Role1 with attribute matches requires on Role2

1 view
Skip to first unread message

Pedro Melo

unread,
Mar 1, 2013, 11:56:57 AM3/1/13
to moose
Hi,

I keep getting bitten by this, and I was wondering if this is
something that will be solved eventually, or its something that would
be nice to solve but it is actually tricky to do, or just that I'm
doing it wrong.

I have two roles, R1 and R2. R1 defines a attribute ctx. R2 requires a 'ctx'.

If I use the two roles on a class C, with a single "with 'R1', 'R2';" I get

'R1|R2' requires the method 'ctx' to be implemented by 'C'

I can work around it with two "with 'R1'; with 'R2';". But I loose all
the other protections wrt duplicate methods I might have in R1 and R2.

Opinions?
--
Pedro Melo
@pedromelo
http://www.simplicidade.org/
xmpp:me...@simplicidade.org
mailto:me...@simplicidade.org

Jesse Luehrs

unread,
Mar 1, 2013, 12:05:34 PM3/1/13
to Pedro Melo, moose
On Fri, Mar 01, 2013 at 04:56:57PM +0000, Pedro Melo wrote:
> Hi,
>
> I keep getting bitten by this, and I was wondering if this is
> something that will be solved eventually, or its something that would
> be nice to solve but it is actually tricky to do, or just that I'm
> doing it wrong.
>
> I have two roles, R1 and R2. R1 defines a attribute ctx. R2 requires a 'ctx'.
>
> If I use the two roles on a class C, with a single "with 'R1', 'R2';" I get
>
> 'R1|R2' requires the method 'ctx' to be implemented by 'C'
>
> I can work around it with two "with 'R1'; with 'R2';". But I loose all
> the other protections wrt duplicate methods I might have in R1 and R2.
>
> Opinions?

This is something that we'd like to fix - the problem is mainly that
role accessors aren't generated until they are composed into a class,
but role application composes roles together before composing them into
the class. What we really need to do is provide a way for roles to say
"I'm going to generate a method of this name", which we would be able to
query during the role summation process (we can't actually generate the
methods themselves because doing so requires an instance metaclass,
which only classes have).

Patches to fix this would be appreciated.

-doy

Ovid

unread,
Mar 4, 2013, 7:12:21 AM3/4/13
to Jesse Luehrs, Pedro Melo, moose
From: Jesse Luehrs <d...@tozt.net>
>To: Pedro Melo <me...@simplicidade.org>
>Cc: moose <mo...@perl.org>
>Sent: Friday, 1 March 2013, 18:05
>Subject: Re: Role1 with attribute matches requires on Role2


Out of curiosity, how is this handled in the internals? Does the role composition do a simple ->can() check for methods? If so, during composition, could has() fake a forward declaration?

    {
        package Foo;
        sub bar;
    }
    say "yes" if Foo->can('bar');

You're not actually creating the method yet, but you're promising that it will be there.

Cheers,
Ovid
--
Twitter - http://twitter.com/OvidPerl/
Buy my book - http://bit.ly/beginning_perl
Buy my other book - http://www.oreilly.com/catalog/perlhks/
Live and work overseas - http://www.overseas-exile.com/

Jesse Luehrs

unread,
Mar 4, 2013, 10:29:06 AM3/4/13
to Ovid, Pedro Melo, moose
Yes, that is another possibility. I'm not sure which way would be
simpler.

-doy
0 new messages