Thread overview
Foreach on a template?
Mar 06, 2007
Robin Allen
Mar 06, 2007
Daniel Keep
Mar 06, 2007
Russell Lewis
Mar 11, 2007
Robin Allen
Mar 13, 2007
Robin Allen
March 06, 2007
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
March 06, 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/
March 06, 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(...) {...}
    }
  }
March 11, 2007
>>
>> 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
March 11, 2007
"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.


March 13, 2007
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. 
> 
>