Foreach on a template?

Daniel Keep daniel.keep.lists at gmail.com
Tue Mar 6 10:54:11 PST 2007


Robin Allen wrote:
> You can define an opApply in a template and use it like this:
> 
> mytemplate!(int).opApply(int delegate(int v) {
> 
>     ...loop...
>     return 0;
> });
> 
> Which is what I had to do since D won't let you do this:
> 
> foreach(v; mytemplate!(int))
> {
>     ...loop...
> }
> 
> The second one looks much nicer, and they both mean the same thing. So
> why do we have to use the first?
> 
> -Rob

Because "foreach(v ; mytemplate!(int))" expects "mytemplate!(int)" to be
either an array or an object which has an opApply overload[*], so
technically they *DON'T* mean the same thing.

	-- Daniel

[*] Incidentally, have you tried specifying a function pointer or
delegate to foreach instead?  Offhand, I think that was implemented a
while back...

-- 
Unlike Knuth, I have neither proven or tried the above; it may not even
make sense.

v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/



More information about the Digitalmars-d mailing list