Foreach on a template?
Russell Lewis
webmaster at villagersonline.com
Tue Mar 6 14:17:14 PST 2007
Daniel Keep wrote:
> 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...
Another possibility is to have the template evaluate to a struct, and to
implement opApply in that struct:
template foo(args) {
struct foo {
int opApply(...) {...}
}
}
More information about the Digitalmars-d
mailing list