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

Anonymous classes

Thread Next
From:
Simon Cozens
Date:
May 29, 2005 02:02
Subject:
Anonymous classes
Message ID:
87br6u4d8b.fsf@alibi.simon-cozens.org

Hello,
    I'm having a seriously good time porting Maypole to Perl 6. If you
still have reservations about how Perl 6 is going to be to program in,
I urge you to try programming in it.
    Now, commercial over, I have some questions.

    What's the syntax for declaring inherited anonymous classes, and
then adding methods to them? 
    I've realised that anonymous classes are an excellent solution to
subclassing *related* classes - for instance, you have a Foo class which
creates Foo::Request and Foo::Response objects, and when you subclass
Foo, you may want to specialise things in Foo::Request and Foo::Response
as well. So instead you go:

    class Foo {
      has Class $.request_class is rw; 
      method BUILD { 
         $.request_class = class is Foo::Request; # Or whatever
      }
    }

And then you have a request class that is a singleton class to each
Foo instantiation, which can be specialized however your particular
Foo subclass needs.

-- 
>Almost any animal is capable learning a stimulus/response association,
>given enough repetition.
Experimental observation suggests that this isn't true if double-clicking
is involved. - Lionel, Malcolm Ray, asr.

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