foreach and filter

Timon Gehr timon.gehr at gmx.ch
Mon Apr 16 05:58:41 PDT 2012


On 04/16/2012 01:20 PM, Russel Winder wrote:
> On Wed, 2012-04-11 at 18:32 +0200, Jakob Ovrum wrote:
> [...]
>> Ranges are iterated in-place. You can't mutate a const range,
>> hence you cannot advance it by one (`popFront`), which is
>> required by the lowering of foreach using the range interface.
> [...]
>
> I think I am still trying to recover from the shock of this; it is very
> likely to turn anyone interested in any form of declarative expression
> away from D. Also the internal activity is being exposed at the API
> level.  Not to mention the history of iteration being one of working
> with mutable object on immutable structures.
>
> Trying to create the smallest possible example:
>
> 	auto r = iota ( 0 , 10 ) ;
> 	writeln ( map ! ( i =>  i * i ) ( r ) ) ;
>
> works fine. Let us try:
>
> 	const r = iota ( 0 , 10 ) ;
> 	writeln ( map ! ( i =>  i * i ) ( r ) ) ;
>
> works fine. Great. Now
>
> 	auto r = iota ( 0 , 10 ) ;
> 	writeln ( reduce ! ( ( a, b ) =>  a + b ) ( 10 , r ) ) ;
>
> works fine. Let's try:
>
> 	const r = iota ( 0 , 10 ) ;
> 	writeln ( reduce ! ( ( a, b ) =>  a + b ) ( 10 , r ) ) ;
>
> Oh dear:
>
> /home/users/russel/lib.Linux.x86_64/DMD2/bin64/../../src/phobos/std/algorithm.d(725): Error: function std.range.iota!(int,int).iota.Result.popFront () is not callable using argument types ()
> /home/users/russel/lib.Linux.x86_64/DMD2/bin64/../../src/phobos/std/algorithm.d(725): Error: function std.range.iota!(int,int).iota.Result.front () is not callable using argument types ()
> example.d(7): Error: template instance example.main.reduce!(__lambda3).reduce!(int,const(Result)) error instantiating
>
> Not only is the error message incomprehensible to someone who doesn't
> know the details of the internal workings (OK not a killer blow), but
> the very inconsistency of behaviour between map and reduce is a
> violation of the Principle of Least Surprise, and seems like a killer
> blow.

Have you filed a bug report against Phobos for this?


More information about the Digitalmars-d mailing list