On Sat, Aug 10, 2002 at 07:30:19PM -0400, Chris Dutton wrote:
> 
> 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;

I'm not exactly sure what you're trying to do with this. You can create
an object within the same statement as you define the class:

    my $foo_obj = class {
        method new {
            # yada yada yada
        }
    }.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

As it stands, you're not gaining anything over:

    sub foo(int $bar //= 0, int $multiply_by //= 1) {
        print $bar * $multiply_by, "\n";
    }

    foo(5,2);

I'll tease out the various threads there. Keep in mind that the new OO
syntax is only partially defined at this point. Yes, you will be able to
return an anonymous class. Yes, it will be lexically scoped to the
context in which it was defined, so it will have access to $bar. Yes,
attributes can have properties attached, and I imagine C<is constant>
will be one of the acceptable properties. I also imagine attributes will
have the option of being initialized using assignment-style (C<=>)
syntax. I also imagine that attributes will be able to be typed. The
current consensus on attribute declaration is to use this syntax:

                attr $.baz;

(i.e. C<attr>, not C<my> or C<our>)
Or in your case:

                attr int $.baz is constant = $bar;



> foo(5).new.out(2); # 10

I suspect this will be allowable syntax. But if you find yourself using
it you might want to re-think the code. Creating a class and an object
for single use isn't tremendously efficient. Then again, you may be
golfing. :)

Allison

Reply via email to