April 16, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7924

           Summary: reduce does not work with immutable/const as map and
                    filter do
           Product: D
           Version: D2
          Platform: x86_64
        OS/Version: Linux
            Status: NEW
          Severity: major
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: russel@winder.org.uk


--- Comment #0 from Russel Winder <russel@winder.org.uk> 2012-04-16 08:23:47 PDT ---
(I have marked this as x86_64/Linux as that is the only platform I have tried this on. I am fairly convinced it is an all/all though.)

The following code:

import std.algorithm ;
import std.range ;
import std.stdio ;

void main ( immutable string[] args ) {
  immutable r = iota ( 0 , 10 ) ;
  writeln ( map ! ( i => i * i ) ( r ) ) ;
  writeln ( filter ! ( i => true ) ( r ) ) ;
  writeln ( reduce ! ( ( a , b ) => a + b ) ( 10 , r ) ) ;
}

fails to compile using DMD 2.059 with the message:

/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 ()
issue_XXXX.d(9): Error: template instance
issue_XXXX.main.reduce!(__lambda7).reduce!(int,immutable(Result)) error
instantiating
Failed: 'dmd' '-v' '-o-' 'issue_XXXX.d' '-I.'

If the immutable is replaced with const the same message ensues with s/immutable/const/.  If the reduce is commented out then it all works:

[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

reduce is thus not consistent with map and filter, which I think has to be considered a serious error.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 20, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=7924


irritate <irritate@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |irritate@gmail.com


--- Comment #1 from irritate <irritate@gmail.com> 2013-06-19 20:17:02 PDT ---
The error message on head revision (DMD 2.064) is different, but this still fails to compile.  The problem here is that reduce() fails to instantiate because of an isIterable constraint check on the immutable iota.

I "unwound" the example into just the iteration:

---
import std.stdio ;

void main()
{
  immutable r = iota(0, 10);
  foreach(elem; r)
  {
      writeln(elem);
  }
}

DMD v2.064 DEBUG
issue_7924.d(8): Error: mutable method std.range.iota!(int,
int).iota.Result.popFront is not callable using a immutable object
---

And popFront mutates a member variable for the current value, so I don't think there's much we can do about this.

SIDE NOTE: map and filter work because they Unqual the iota when they wrap it. However they also have non-const popFront, and making them immutable directly also leads to issues because of that:

---
import std.stdio ;

void main()
{
  immutable r = map!(i => i*i)(iota(0, 10));
  foreach(elem; r)
  {
      writeln(elem);
  }
}
DMD v2.064 DEBUG
issue_7924.d(7): Error: cannot implicitly convert expression (map(iota(0, 10))
of type MapResult!(__lambda2, Result) to immutable(MapResult!(__lambda2,
Result)
)
---

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------