Re: Role1 with attribute matches requires on Role2

[email protected] (Ovid)
Newsgroups perl.moose
Message-ID <[email protected]>
From: Jesse Luehrs <[email protected]>
>To: Pedro Melo <[email protected]> 
>Cc: moose <[email protected]> 
>Sent: Friday, 1 March 2013, 18:05
>Subject: Re: Role1 with attribute matches requires on Role2
> 
>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.


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/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.