On Saturday, August 10, 2002, at 06:25 PM, Piers Cawley wrote:

> Chris Dutton <[EMAIL PROTECTED]> writes:
>
>> Since Adam Lopesto asked a non-regex question, I don't feel quite as
>> out of place for doing the same.
>>
>> This one actually came to me just the other night.  Would it be
>> possible in Perl 6 to create "anonymous classes"?  Something like:
>>
>> my $foo_class = class {
>>      method new {
>>              # yada yada yada
>>      }
>> }
>>
>> my $foo_obj = $foo_class.new;
>
> I hope so.

The only problem I could see, and I wanted to wait for at least one 
other opinion before mentioning this, is rewriting the above as:

my $foo_class $foo_obj = $foo_class.new;

Still I can see this as occasionally being useful for having instance 
variables which can effectively be initialized, but afterwards are 
constant.  Then again, there's probably another, more graceful way to do 
this using the new method and properties.

sub foo(int $bar //= 0) {
        return class {
                int $.baz is constant = $bar;
                method out(int $multiply_by //= 1) {
                        print $.baz * $multiply_by, "\n";
                }
        }
}

foo(5).new.out(2); # 10
foo(6).new.out(3); # 18

Reply via email to