On Mon, Mar 24, 2003 at 06:29:10AM -0800, Austin Hastings wrote:
: 
: --- arcadi shehter <[EMAIL PROTECTED]> wrote:
: > is there any chance for this to work :
: > 
: > sub new_counter($start=0) { 
: >     return sub { 
: >              prop $cnt =  $start; #this is opposite to "state"
: >                                   #which sets trait of the block ,
: >                                   #so presumably , this is created 
: >                                   #anew every time closure is created
: >              return ++$cnt;
: >            }         
: > }
: > 
: 
: Interesting notion. However, given that $cnt is "static", this seems
: like one of those places where a good optimizer might always return the
: same sub -- treating the entire thing as a constant expression. 

Depends on when the initialization is done.

: So this is really a kind of semantic question: does the sub on the rhs
: always imply run time consideration, or might a sub-expr be considered
: a constant and be folded?

The purpose of a state variable is to keep state across multiple calls
to the same scope, so I'd say the proper semantics on closures is
to treat the generation of a closure as a new block with new state properties.
The most useful initialization semantics appear to be "just in time",
that is, on first actual call to the generated closure.  START time
in my previous message, though I'm still unhappy with that name.
FIRST would be better, but that's taken (this week).

A START-time initialization would certainly prevent the optimizer from
returning a constant closure above, whereas an init done at BEGIN,
CHECK, or INIT time would result in a constant closure, or maybe an
error if the compiler determines that you're initializing with a
value that is undefined before the first call.

: (This has interesting implictations for simple generators, since the
: above syntax is short and sweet, while the usual "make it an object"
: implementation looks stupid and ungainly.)

Certainly.

Larry

Reply via email to