Re: How do I make my class iterable?

2015-06-22 Thread Assembly via Digitalmars-d-learn
On Monday, 22 June 2015 at 20:34:00 UTC, anonymous wrote: On Monday, 22 June 2015 at 18:44:22 UTC, Assembly wrote: I'm using this, thanks for all. Can someone clarify how does opApply() works? I assume it's called every iteration and as opApply() has a loop does it means the number of iteration

Re: How do I make my class iterable?

2015-06-22 Thread anonymous via Digitalmars-d-learn
On Monday, 22 June 2015 at 18:44:22 UTC, Assembly wrote: I'm using this, thanks for all. Can someone clarify how does opApply() works? I assume it's called every iteration and as opApply() has a loop does it means the number of iteration ran actually is the ones from foreach() is 2*n where n is

Re: How do I make my class iterable?

2015-06-22 Thread Ali Çehreli via Digitalmars-d-learn
On 06/22/2015 11:48 AM, Assembly wrote: > if my opApply() is defiend as the following: > > int opApply(int delegate(ref int, ref T) del) > { [...] > } > > and called like: > > foreach(int i, MyType p; places) { > > isn't i the loop counter? size_t is more natural but yes, that's it. A

Re: How do I make my class iterable?

2015-06-22 Thread Assembly via Digitalmars-d-learn
On Monday, 22 June 2015 at 18:07:36 UTC, Ali Çehreli wrote: On 06/22/2015 10:03 AM, Assembly wrote: > foreach(int i, MyType p; places) { > > but I get this error: > > Error: cannot infer argument types, expected 1 argument, not 2 Yeah, the loop counter is automatic only for slices. You can use

Re: How do I make my class iterable?

2015-06-22 Thread Assembly via Digitalmars-d-learn
On Monday, 22 June 2015 at 17:09:16 UTC, Steven Schveighoffer wrote: On 6/22/15 1:03 PM, Assembly wrote: [...] TBH, opApply is much better suited to classes. But in order to have multiple parameters with foreach by using a range, you must return a tuple: auto front() { import std.typecons:

Re: How do I make my class iterable?

2015-06-22 Thread Ali Çehreli via Digitalmars-d-learn
On 06/22/2015 10:03 AM, Assembly wrote: > foreach(int i, MyType p; places) { > > but I get this error: > > Error: cannot infer argument types, expected 1 argument, not 2 Yeah, the loop counter is automatic only for slices. You can use 'enumerate' for other types: foreach (i, element; Numb

Re: How do I make my class iterable?

2015-06-22 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/22/15 1:03 PM, Assembly wrote: On Monday, 22 June 2015 at 16:52:15 UTC, Ali Çehreli wrote: On 06/22/2015 09:37 AM, q66 wrote: use opApply. Yes. Additionally, an InputRange interface can be used: http://ddili.org/ders/d.en/foreach_opapply.html Ali I was reading exaclty this page th

Re: How do I make my class iterable?

2015-06-22 Thread Assembly via Digitalmars-d-learn
On Monday, 22 June 2015 at 16:52:15 UTC, Ali Çehreli wrote: On 06/22/2015 09:37 AM, q66 wrote: use opApply. Yes. Additionally, an InputRange interface can be used: http://ddili.org/ders/d.en/foreach_opapply.html Ali I was reading exaclty this page that. I've had implmented this method/

Re: How do I make my class iterable?

2015-06-22 Thread Ali Çehreli via Digitalmars-d-learn
On 06/22/2015 09:37 AM, q66 wrote: use opApply. Yes. Additionally, an InputRange interface can be used: http://ddili.org/ders/d.en/foreach_opapply.html Ali

Re: How do I make my class iterable?

2015-06-22 Thread q66 via Digitalmars-d-learn
On Monday, 22 June 2015 at 16:33:43 UTC, Assembly wrote: Does D has an equivalent to C#'s iterator (https://msdn.microsoft.com/en-us/library/65zzykke.aspx)? if so, where can I find it? What I want is loop over a user-defined class/struct. In case of C#, I just implement the IEnumerable and th

How do I make my class iterable?

2015-06-22 Thread Assembly via Digitalmars-d-learn
Does D has an equivalent to C#'s iterator (https://msdn.microsoft.com/en-us/library/65zzykke.aspx)? if so, where can I find it? What I want is loop over a user-defined class/struct. In case of C#, I just implement the IEnumerable and the GetEnumerator() methods that's called by the foreach()