A const idiom + a different 'delete'

Pillsy pillsbury at gmail.com
Thu Aug 12 10:36:41 PDT 2010


Shin Fujishiro Wrote:

> bearophile <bearophileHUGS at lycos.com> wrote:
> > This is an alternative way to write it that I've never used 
> < because I don't like it much:

> > void main() {
> >     const(int[int]) aa = {
> >         int[int] result;
> >         foreach (i; 0 .. 10)
> >             result[i] = i * i;
> >         return result;
> >     }();
> > }

> It looks like LISP's (let ...) expression and personally I like it.
> The idiom is really useful for building immutable things, including
> compile-time constants.

It also makes for a fine replacement for the comma operator, because you can use it anywhere you can use an expression. 

I'd love a bit of syntactic sugar for the idiom, that would do the same thing the Lisp LET does, transforming

   let (x = a, y = b) { foo(x); bar(y); return quux(x, y); } 

into 

   (x, y){ foo(x); bar(y); return quux(x, y); }(a, b)

automatically.

Cheers,
Pillsy


More information about the Digitalmars-d mailing list