www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Unable to call each on a lockstep range containing 2 or more ranges

reply maik klein <maikklein googlemail.com> writes:
https://stackoverflow.com/questions/33779822/unable-to-call-each-on-a-lockstep-range-containing-2-or-more-ranges

http://dpaste.dzfl.pl/76c79f1f12ab

void main(){
   import std.container;
   import std.stdio;
   import std.algorithm.iteration;
   import std.range;
   Array!int ai = [1,2,3,4];
   Array!int ai1 = [1,2,3,4];
   Array!int ai2 = [1,2,3,4];

   auto arange = lockstep(ai[],ai1[]);
   arange.each!((a,b) => writeln(a, b));

   auto arange2 = lockstep(ai[],ai1[],ai2[]);
   arange2.each!((a,b,c) => writeln(a, b, c));
}

Error: template std.algorithm.iteration.each cannot deduce 
function from argument types !((a, b, c) => writeln(a, b, 
c))(Lockstep!(RangeT!(Array!int), RangeT!(Array!int), 
RangeT!(Array!int))), candidates are: 
/opt/compilers/dmd2/include/std/algorithm/iteration.d(820):
std.algorithm.iteration.each(alias pred = "a")


"arange" works but "arange2" doesn't because the compiler is 
unable to deduce the the function. The error even appears if I 
explicitly add the argument types.
Nov 18 2015
next sibling parent reply John Colvin <john.loughran.colvin gmail.com> writes:
On Wednesday, 18 November 2015 at 12:20:42 UTC, maik klein wrote:
 https://stackoverflow.com/questions/33779822/unable-to-call-each-on-a-lockstep-range-containing-2-or-more-ranges

 http://dpaste.dzfl.pl/76c79f1f12ab

 void main(){
   import std.container;
   import std.stdio;
   import std.algorithm.iteration;
   import std.range;
   Array!int ai = [1,2,3,4];
   Array!int ai1 = [1,2,3,4];
   Array!int ai2 = [1,2,3,4];

   auto arange = lockstep(ai[],ai1[]);
   arange.each!((a,b) => writeln(a, b));

   auto arange2 = lockstep(ai[],ai1[],ai2[]);
   arange2.each!((a,b,c) => writeln(a, b, c));
 }

 Error: template std.algorithm.iteration.each cannot deduce 
 function from argument types !((a, b, c) => writeln(a, b, 
 c))(Lockstep!(RangeT!(Array!int), RangeT!(Array!int), 
 RangeT!(Array!int))), candidates are: 
 /opt/compilers/dmd2/include/std/algorithm/iteration.d(820):
 std.algorithm.iteration.each(alias pred = "a")


 "arange" works but "arange2" doesn't because the compiler is 
 unable to deduce the the function. The error even appears if I 
 explicitly add the argument types.
I think this is a bug, please report it at issues.dlang.org and perhaps there will be an explanation or it will be fixed. In the mean time, something like this should work: auto arange2 = zip(ai[],ai1[],ai2[]); arange2.each!((t) => writeln(t[0], t[1], t[2])); // or if you really must have the names: arange2.each!((t) => (a,b,c){ writeln(a, b, c); }(t.expand));
Nov 18 2015
parent reply maik klein <maikklein googlemail.com> writes:
On Wednesday, 18 November 2015 at 13:51:59 UTC, John Colvin wrote:
 On Wednesday, 18 November 2015 at 12:20:42 UTC, maik klein 
 wrote:
 [...]
I think this is a bug, please report it at issues.dlang.org and perhaps there will be an explanation or it will be fixed. In the mean time, something like this should work: auto arange2 = zip(ai[],ai1[],ai2[]); arange2.each!((t) => writeln(t[0], t[1], t[2])); // or if you really must have the names: arange2.each!((t) => (a,b,c){ writeln(a, b, c); }(t.expand));
Thanks, but the problem I have with zip is that it doesn't work with "ref". for example auto arange3 = zip(ai[],ai1[],ai2[]); foreach(ref a; arange3){ a[0] = 42; } Won't change anything. Is this another bug?
Nov 18 2015
parent John Colvin <john.loughran.colvin gmail.com> writes:
On Wednesday, 18 November 2015 at 14:11:45 UTC, maik klein wrote:
 On Wednesday, 18 November 2015 at 13:51:59 UTC, John Colvin 
 wrote:
 On Wednesday, 18 November 2015 at 12:20:42 UTC, maik klein 
 wrote:
 [...]
I think this is a bug, please report it at issues.dlang.org and perhaps there will be an explanation or it will be fixed. In the mean time, something like this should work: auto arange2 = zip(ai[],ai1[],ai2[]); arange2.each!((t) => writeln(t[0], t[1], t[2])); // or if you really must have the names: arange2.each!((t) => (a,b,c){ writeln(a, b, c); }(t.expand));
Thanks, but the problem I have with zip is that it doesn't work with "ref". for example auto arange3 = zip(ai[],ai1[],ai2[]); foreach(ref a; arange3){ a[0] = 42; } Won't change anything. Is this another bug?
Unfortunately, yes: https://issues.dlang.org/show_bug.cgi?id=10541 Note that while(!arange3.empty) { arange3.front[0] = 42; arange3.popFront(); } should work as expected.
Nov 18 2015
prev sibling parent reply Meta <jared771 gmail.com> writes:
On Wednesday, 18 November 2015 at 12:20:42 UTC, maik klein wrote:
 https://stackoverflow.com/questions/33779822/unable-to-call-each-on-a-lockstep-range-containing-2-or-more-ranges

 http://dpaste.dzfl.pl/76c79f1f12ab

 void main(){
   import std.container;
   import std.stdio;
   import std.algorithm.iteration;
   import std.range;
   Array!int ai = [1,2,3,4];
   Array!int ai1 = [1,2,3,4];
   Array!int ai2 = [1,2,3,4];

   auto arange = lockstep(ai[],ai1[]);
   arange.each!((a,b) => writeln(a, b));

   auto arange2 = lockstep(ai[],ai1[],ai2[]);
   arange2.each!((a,b,c) => writeln(a, b, c));
 }

 Error: template std.algorithm.iteration.each cannot deduce 
 function from argument types !((a, b, c) => writeln(a, b, 
 c))(Lockstep!(RangeT!(Array!int), RangeT!(Array!int), 
 RangeT!(Array!int))), candidates are: 
 /opt/compilers/dmd2/include/std/algorithm/iteration.d(820):
 std.algorithm.iteration.each(alias pred = "a")


 "arange" works but "arange2" doesn't because the compiler is 
 unable to deduce the the function. The error even appears if I 
 explicitly add the argument types.
Which version of the compiler are you using?
Nov 18 2015
parent reply maik klein <maikklein googlemail.com> writes:
On Wednesday, 18 November 2015 at 17:22:52 UTC, Meta wrote:
 On Wednesday, 18 November 2015 at 12:20:42 UTC, maik klein 
 wrote:
 [...]
Which version of the compiler are you using?
Linux - DMD64 D Compiler v2.069.0
Nov 18 2015
parent Meta <jared771 gmail.com> writes:
On Wednesday, 18 November 2015 at 17:40:21 UTC, maik klein wrote:
 On Wednesday, 18 November 2015 at 17:22:52 UTC, Meta wrote:
 Which version of the compiler are you using?
Linux - DMD64 D Compiler v2.069.0
https://issues.dlang.org/show_bug.cgi?id=15357 https://issues.dlang.org/show_bug.cgi?id=15358
Nov 18 2015