On Monday, August 12, 2002, at 01:27 PM, Allison Randal wrote:
> 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.
Sort of like:
class A { ... }
my A $a = A.new;
Where A is $foo_class. Probably not terribly useful, but I just
wondered if it'd work anyway.
> You can create
> an object within the same statement as you define the class:
>
> my $foo_obj = class {
> method new {
> # yada yada yada
> }
> }.new;
I figured that would work, but wasn't entirely sure. Thanks.
>> 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);
Granted, it was a trivial example. :-)
> attr int $.baz is constant = $bar;
I like it.
>> 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. :)
Hmmm.... could I have instead written the following and ended up with
both a lexically scoped anonymous class($fc), and an instance($fo)?
( my $fo = ( my $fc = foo(5) ).new ).out;
One thing is obvious... it's been way too long since I read the various
Exegesis documents.