opApply ref and "const" iterators
Jarrett Billingsley
jarrett.billingsley at gmail.com
Mon Sep 28 16:51:28 PDT 2009
On Mon, Sep 28, 2009 at 6:58 PM, gzp <galap at freemail.hu> wrote:
> 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 ?
Yes. For some reason the delegate that opApply takes must always take
ref params, even if you don't iterate as such. This isn't really
documented anywhere.
More information about the Digitalmars-d
mailing list