Re: Expression-local variables and 'let'

Bryn Keller <[email protected]> Sun, 22 Feb 2004 01:09:34 -0800
Newsgroups gmane.comp.lang.nice.general
Message-ID <[email protected]>
Daniel Bonniot wrote:

>
>>> You can call the body multiple times. 'foreach' is implemented in 
>>> nice.lang, for instance.
>>> Am I missing something?
>>>   
>>
>>
>> Yes, you can execute the body multiple times but you can only execute 
>> the
>> condition once. The type of a 'while' function would be:
>>     (() -> bool, () -> ()) -> ()
>>
>> But, the calling syntax only works for a function of type:
>>     (bool, () -> ()) -> ().
>>  
>>
> You're right.
>
> One way to handle this would be to allow a declaration like:
>  void while(delayed<boolean> condition, delayed<void> body) { ... }
>
> delayed<T> would be basically ()->T, but with the additional rule that 
> an expression of type T can be used where delayed<T> is expected 
> (semantically, it is wrapped inside a closure at that point, and so 
> it's value is not evaluated). It's probably needed to introduce those 
> delayed types because I would think it's unwise to silently convert 
> from T to ()->T everywhere.
>
> I first thought about using the name lazy<T>, but that's actually a 
> different concept (once the value is computed, you "cache" it, not 
> recompute it at each use).

It is a different concept - the concept is 'generator'. The while loop:

int i = 0;
while(i++ < 5) {
  println("Hello");
}

is exactly equivalent to:

  int i = 0;
  (()=> i++ < 5).foreach() {
    println("Hello");
  }

Using the new generator library plus a slightly different version of 
foreach that I'll add to the generator library now. Having syntactic 
support for generators isn't a bad idea, IMHO.

Bryn





-------------------------------------------------------
SF.Net is sponsored by: Speed Start Your Linux Apps Now.
Build and deploy apps & Web services for Linux with
a free DVD software kit from IBM. Click Now!
http://ads.osdn.com/?ad_id=1356&alloc_id=3438&op=click