foreach and filter

Simen Kjaeraas simen.kjaras at gmail.com
Wed Apr 11 09:33:39 PDT 2012


On Wed, 11 Apr 2012 18:08:14 +0200, Russel Winder <russel at winder.org.uk>  
wrote:

> Doing something along the lines of:
>
> 	const a = array ( filter! ... ) ;
> 	foreach ( i ; a ) { ... }
>
> works fine.  Question 1 though is why I can't use immutable here, why I
> have to use const.  Question 2 is why I can't do:
>
> 	const a = filter! ... ;
> 	foreach ( i ; a ) { ... }

The answer to #1 is easy. Nothing is implicitly castable to immutable.

The easy answer to #2 is that anything const cannot be a range, because
popFront cannot be const.

I have (long ago, probably not usable in this day and age), written an
implementation of tail const for ranges, but it is invasive[1].

For now, the solution is simply to not use const/immutable with ranges
other than arrays.


Some solution really should be created for this problem. One simple
solution would be for all ranges to support opSlice with an empty
parameter list, which would return a non-const version of the range.
The problems are:

1. The elements should still be const.

2. If this range wraps another range, that other range should also
be sliced, and so on.


[1]http://d.puremagic.com/issues/show_bug.cgi?id=5377


More information about the Digitalmars-d mailing list