NotNull pointers

Simen Kjaeraas simen.kjaras at gmail.com
Wed Aug 31 02:08:55 PDT 2011


On Wed, 31 Aug 2011 01:11:20 +0200, bearophile <bearophileHUGS at lycos.com>  
wrote:

> Andrei Alexandrescu:
>
>> new Type[n](argument1, argument2, ..., argumentn)
>
> More musings about this.
>
> A generic lazy/eager array comprehension syntax seems better, like (for  
> the eager one):
> [Baz(x, x*y) foreach(x; 0 .. 10) if (isGood(x))]

I think this warrants discussion. Earlier, I have implemented rudimentary
range comprehensions that look like this:

     list!"2 * a" | iota(10) & where!"a & 1"

It works, for simple cases.

In my opinion, any D array/list/range comprehension should work on
ranges as its base, so foreach is out. Instead, a range should be the
base for the comprehension.

A simple solution would thus be:

     [(x){return 2 * x;}; iota(10); (x){return x & 1;}]

This could even be implemented in a library:

     list((int x){return 2 * x;}, iota(10), (int x){return x & 1;})

However, as anyone can see, this is crap. It uses delegates, is waay
too verbose, and hurts my eyes. (It could be somewhat better with the
functions passed as template alias parameters, but that would mess
up the order of parameters, and on the whole not help that much)
Next iteration:

     [x; 2 * x; iota(10); x & 1]

This is a syntax I could accept. I would like to have the x be more
closely associated with the range, something more akin to this:

     [2 * x; x <= iota(10); x & 1]

I am however at a loss as to what combination of squiggles could be
used in place of the <= (which is of course not a good choice).

The following variations should be allowed:

     [x; ; iota(10); x & 1]; // (or [; x <= iota(10); x & 1])
     [x; 2 * x; iota(10); ]; // (or [2 * x; x <= iota(10); ])

With the first being simply a filter, the second simply a map.

-- 
   Simen


More information about the Digitalmars-d mailing list