develooper Front page | perl.perl6.language | Postings from May 2005

Re: Virtual methods

Thread Previous | Thread Next
From:
Luke Palmer
Date:
May 18, 2005 07:51
Subject:
Re: Virtual methods
Message ID:
7ca3f01605051807516ecc7fb4@mail.gmail.com
On 5/18/05, Aaron Sherman <ajs@ajs.com> wrote:
> In Perl 6, I don't think we need to tag methods as "virtual" like C++
> does, since we have the handy yadda, yadda to do that for us.
> 
> However, there is a variant of C++'s virtual that I'd love to see. By
> default a role cannot override the methods of a class, but if it could
> override those methods specifically marked with the virtual trait, then
> we could define stub methods in classes that don't have a specific
> behavior until a more concrete role is mixed in.
> 
> This gives you a form of auto-loading like delegation, but with less
> storage overhead (since there's no encapsulation until you need it).
> Here's an example:
> 
>         role X {
>                 has Str $.string handles<ucfirst>;
>                 # I'll write something like an accessor to avoid brining
>                 # up some questions around how virtual methods interact
>                 # with auto-accessors just yet.
>                 method setstring(Str $string) { $.string = $string }
>         }
>         class Y {
>                 method setstring(Y $me: Str $string)
>                     is virtual {
>                         $me does X;

Except that mixins like this always treat things as "virtual". 
Whenever you mixin a role at runtime, Perl creates an empty, anonymous
subclass of the current class and mixes the role in that class.  Since
roles beat superclasses, you'll always override your own methods.

Luke

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About