in reply to How will you use state declared variables in Perl6?

I really want to reply here, but I am having a hard time thinking of a good example. It's not that I wouldn't use state, but that I would use it so often and instinctively that I can't really come up with anything of note. It would be like asking me for a "good example" of using the assignment operator. It's the kind of subconscious thing where I just use it when I need it.

That said, you [update: and mirod -- I must have missed his reply] already described the general circumstances when I would use state. Basically, any time I would reach for a bare-block-scoped lexical to create a closure. So instead of the Perl5esque:

{ my $foo; sub foo { $foo += @_; } }

I'd use:

sub foo { state $foo; $foo += @_; }

Which -- while maybe not a big win -- I think is pretty nice.

Update: BUU makes a good point.

Replies are listed 'Best First'.
Re^2: How will you use state declared variables in Perl6?
by BUU (Prior) on Jul 09, 2005 at 02:47 UTC
    You say "maybe not a big win", but what about something like:
    foo(); { my $foo = 42; sub foo { print "Foo: $foo" } }
    Which I've done all too often.