each & opApply

Alex sascha.orlov at gmail.com
Wed May 23 13:37:37 UTC 2018

This is a question is about usage of

with a type where different opApply overloads are defined. Say, I 
have something like this:

void main()
	import std.stdio : writeln;
	import std.algorithm : each;

	auto c = Container();
	c.arr1.length = 50;
	c.arr2.length = 5;

	c.each!((a, b) => writeln(a, b));
         //c.each!(a => writeln(a)); // why this line does not 

struct El1{}
struct El2{}

struct Container
	El1[] arr1;
	El2[] arr2;

	int opApply(int delegate(ref El1, ref El2) operations){ 
assert(0); }
	int opApply(int delegate(ref El2) operations){ assert(0); }
	int opApply(int delegate(ref El1) operations){ assert(0); }
	int opApply(int delegate(ref El2, ref El1) operations){ 
assert(0); }

The compilation error on the last line in the main is:

/usr/local/opt/dmd/include/dlang/dmd/std/algorithm/iteration.d(966,21): Error: template `D main.__lambda2` cannot deduce function from argument types `!()(El1, El2)`, candidates are:
source/app.d(12,13):        `app.main.__lambda2`
source/app.d(12,6): Error: template instance `app.main.each!((a) 
=> writeln(a)).each!(Container)` error instantiating

So... I get the idea, that ´each´ looks only on the first opApply 
overload, right?
Is there any possibility, to convince it to use a specific one? 
Say, for the last line in the main, to use the third overload of 

By the way, iterating via foreach works as expected: each of

foreach(El1 el; c){}
foreach(El2 el; c){}
foreach(El1 el1, El2 el2; c){}
foreach(El2 el1, El1 el2; c){}

compiles and iterates as it should.

More information about the Digitalmars-d-learn mailing list