On 5/18/05, Aaron Sherman <[EMAIL PROTECTED]> 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

Reply via email to