www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Foreach on a template?

reply Robin Allen <r.a3 ntlworld.com> writes:
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
Mar 06 2007
parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
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/
Mar 06 2007
parent reply Russell Lewis <webmaster villagersonline.com> writes:
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(...) {...} } }
Mar 06 2007
parent reply Robin Allen <r.a3 ntlworld.com> writes:
 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.


Okay, I should have said they *should* mean the same thing. Or that the programmer using foreach would *mean* the same thing. Anyway, acording to the spec, foreach expects an 'expression' which can be pretty much anything, so there's no reason why a template shouldn't work.
     -- Daniel

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


I haven't, but it's not what I'm trying to do here.
 
 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(...) {...}
     }
   }

I can't do that with my template, it's got lots of other stuff in it. And it wouldn't be much neater than what I'm having to do now anyway. -Rob
Mar 11 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Robin Allen" <r.a3 ntlworld.com> wrote in message 
news:et1cch$2gmi$1 digitalmars.com...
 Okay, I should have said they *should* mean the same thing. Or that the 
 programmer using foreach would *mean* the same thing. Anyway, acording to 
 the spec, foreach expects an 'expression' which can be pretty much 
 anything, so there's no reason why a template shouldn't work.

Just because it expects an expression doesn't mean it should be able to handle *any* expression. What about foreach(x; 5) ? That makes no sense. The grammar of foreach just defines what can come there syntactically. It's the semantics that determine what's _legal_ there.
Mar 11 2007
parent Robin Allen <r.a3 ntlworld.com> writes:
I thought the person I was replying to was telling me it couldn't accept 
templates because of the grammar. If I was wrong about that and Daniel 
was just saying that it *doesn't* accept templates: I know, that's what 
I was asking to have changed.

-Rob


Jarrett Billingsley wrote:
 "Robin Allen" <r.a3 ntlworld.com> wrote in message 
 news:et1cch$2gmi$1 digitalmars.com...
 Okay, I should have said they *should* mean the same thing. Or that the 
 programmer using foreach would *mean* the same thing. Anyway, acording to 
 the spec, foreach expects an 'expression' which can be pretty much 
 anything, so there's no reason why a template shouldn't work.

Just because it expects an expression doesn't mean it should be able to handle *any* expression. What about foreach(x; 5) ? That makes no sense. The grammar of foreach just defines what can come there syntactically. It's the semantics that determine what's _legal_ there.

Mar 12 2007