digitalmars.D - Foreach on a template?
- Robin Allen (13/13) Mar 06 2007 You can define an opApply in a template and use it like this:
- Daniel Keep (13/32) Mar 06 2007 Because "foreach(v ; mytemplate!(int))" expects "mytemplate!(int)" to be
- Russell Lewis (8/38) Mar 06 2007 Another possibility is to have the template evaluate to a struct, and to...
- Robin Allen (8/26) Mar 11 2007 Okay, I should have said they *should* mean the same thing. Or that the
- Jarrett Billingsley (8/12) Mar 11 2007 Just because it expects an expression doesn't mean it should be able to
- Robin Allen (6/24) Mar 12 2007 I thought the person I was replying to was telling me it couldn't accept...
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
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? -RobBecause "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
Daniel Keep wrote:Robin Allen wrote: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(...) {...} } }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? -RobBecause "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...
Mar 06 2007
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.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.I haven't, but it's not what I'm trying to do here.-- 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(...) {...} } }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
"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
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