opApply ref and "const" iterators

gzp galap at freemail.hu
Mon Sep 28 15:58:01 PDT 2009


Hello,

Iám new to D so i might be all wrong. Given the code:

class Foo {
	int opApply( int delegate(ref real) dg ) {...} //A. 
	int opApply( int delegate(real) dg ) {...} // B,
}

...
	Foo foo = new Foo;
	foreach( a; foo ) {...}  // 1.
	foreach( ref a; foo ) {...} // 2,

Is it normal that, the A. version (with ref) is called for both cases and if the A is eliminated, only the 2. foreach loop will compile ? The second case works as i expected but what about the first case - the B. function won't be called ever ?

Furthermore, i wanted to create a const iterator

class Foo {
	int opApply( int delegate(ref real) dg ) {...} //A. 
	const int opApply( int delegate(real) dg ) {...} // B,
}

...
	Foo foo = new Foo;
        const Foo f2 = foo;
	foreach( a; f2 ) {...}  // 1.
	foreach( ref a; foo ) {...} // 2,

It won't compile. (dmd 2.032) (I expected that, for the 1. loop the B version and for the 2. loop the A version of the functions'd  be called) 

So what is the D way to create const iterator ?



More information about the Digitalmars-d mailing list