Anonymous classes

18 views
Skip to first unread message

Simon Cozens

unread,
May 29, 2005, 5:01:56 AM5/29/05
to perl6-l...@perl.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.

Ingo Blechschmidt

unread,
May 29, 2005, 9:50:55 AM5/29/05
to perl6-l...@perl.org
Hi,

Simon Cozens wrote:
> 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.

:)

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

I think the only thing you're missing are two braces:
$.request_class = class is Foo::Request {};


--Ingo

--
Linux, the choice of a GNU | self-reference, n. - See self-reference
generation on a dual AMD |
Athlon! |

Simon Cozens

unread,
May 29, 2005, 10:52:59 AM5/29/05
to perl6-l...@perl.org
ibl...@web.de (Ingo Blechschmidt) writes:
> I think the only thing you're missing are two braces:
> $.request_class = class is Foo::Request {};

Thank you; then how do I put methods into $.request_class?

--
"I will make no bargains with terrorist hardware."
-- Peter da Silva

Ingo Blechschmidt

unread,
May 29, 2005, 11:17:49 AM5/29/05
to perl6-l...@perl.org
Hi,

Simon Cozens wrote:

> ibl...@web.de (Ingo Blechschmidt) writes:
>> I think the only thing you're missing are two braces:
>> $.request_class = class is Foo::Request {};
>
> Thank you; then how do I put methods into $.request_class?

$.request_class = class is Foo::Request {

method blarb (...) {...}
...;
};

# or:


$.request_class = class is Foo::Request {};

...;
# later (mixin of an anonymous role):
$.request_class does role {
method blarb (...) {...}
...;
};


--Ingo

--
Linux, the choice of a GNU | We are Pentium of Borg. Division is futile.
generation on a dual AMD | You will be approximated.
Athlon! |

Reply all
Reply to author
Forward
0 new messages