digitalmars.D.learn - How to iterate using foreach on a class?
- Gary Willoughby (4/4) Nov 01 2013 I have a class which contains an array as a core collection of
- simendsjo (3/8) Nov 01 2013 alias this on the underlying array, or implement opApply:
- Gary Willoughby (2/11) Nov 01 2013 Ah that's it, opApply! ta.
- Gary Willoughby (2/10) Nov 01 2013 Alias this?
- evilrat (17/28) Nov 01 2013 alias this can be used to do implicit casting like this
- Jonathan M Davis (16/20) Nov 01 2013 In general, if you want to make something work with foreach, you either ...
- Gary Willoughby (3/33) Nov 01 2013 Hmmm, that's simpler too. ta.
- simendsjo (7/37) Nov 01 2013 So we basically have 4 ways..?
- John Colvin (3/5) Nov 01 2013 arguably these are the same in the context of the foreach loop.
- Dmitry Olshansky (12/17) Nov 01 2013 No quite. I'd say alias this is frankly a bad idea to provide iteration.
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (5/8) Nov 01 2013 As another shameless plug, those foreach methods are included in this
- Philippe Sigaud (5/7) Nov 01 2013 OMG. I just grokked opApply.
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (5/12) Nov 01 2013 You made my day! :) I can't believe that the king of D templates learned...
- Philippe Sigaud (8/12) Nov 01 2013 ub.com/PhilippeSigaud/D-templates-tutorial>
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (13/14) Nov 01 2013 It is a completely static web site (except the Google translate widget)
- Philippe Sigaud (22/35) Nov 02 2013 This is something I'm looking for!
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (19/25) Nov 02 2013 They are the ones that end with .ddoc here:
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (14/17) Nov 03 2013 One annoyance is with parentheses in code sections. Ddoc allows using
- Philippe Sigaud (7/23) Nov 03 2013 c
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (17/20) Nov 03 2013 Actual comma inside a macro is taken as argument separator. From
- Johannes Pfau (6/19) Nov 03 2013 I just saw a small mistake on that page:
- Jonathan M Davis (13/31) Nov 01 2013 Pretty much anytime that you're tempted to use alias this, you should
- Nicolas Sicard (4/45) Nov 03 2013 How about having a nested struct implementing a range interface +
- Jonathan M Davis (10/20) Nov 05 2013 alias this should be avoided as much as possible. It's a bug magnet -
I have a class which contains an array as a core collection of data. I want to pass an instance of this class to a foreach loop and iterate through the enclosed array. How do i do this? I've asked this before and got an answer but i can't find anything now.
Nov 01 2013
On Friday, 1 November 2013 at 11:30:12 UTC, Gary Willoughby wrote:I have a class which contains an array as a core collection of data. I want to pass an instance of this class to a foreach loop and iterate through the enclosed array. How do i do this? I've asked this before and got an answer but i can't find anything now.alias this on the underlying array, or implement opApply: http://dlang.org/statement.html
Nov 01 2013
On Friday, 1 November 2013 at 11:35:03 UTC, simendsjo wrote:On Friday, 1 November 2013 at 11:30:12 UTC, Gary Willoughby wrote:Ah that's it, opApply! ta.I have a class which contains an array as a core collection of data. I want to pass an instance of this class to a foreach loop and iterate through the enclosed array. How do i do this? I've asked this before and got an answer but i can't find anything now.alias this on the underlying array, or implement opApply: http://dlang.org/statement.html
Nov 01 2013
On Friday, 1 November 2013 at 11:35:03 UTC, simendsjo wrote:On Friday, 1 November 2013 at 11:30:12 UTC, Gary Willoughby wrote:Alias this?I have a class which contains an array as a core collection of data. I want to pass an instance of this class to a foreach loop and iterate through the enclosed array. How do i do this? I've asked this before and got an answer but i can't find anything now.alias this on the underlying array
Nov 01 2013
On Friday, 1 November 2013 at 11:39:15 UTC, Gary Willoughby wrote:On Friday, 1 November 2013 at 11:35:03 UTC, simendsjo wrote:alias this can be used to do implicit casting like this ----------------- void someFunc(int x) { ... } struct A { alias this someValue; int someValue; } void main() { A a = A(5); someFunc(a); // implicitly extracts 5 from struct A } --------------- as side note, alias this is not finished yet, so use with caution as it still allows only one alias this per class/structOn Friday, 1 November 2013 at 11:30:12 UTC, Gary Willoughby wrote:Alias this?I have a class which contains an array as a core collection of data. I want to pass an instance of this class to a foreach loop and iterate through the enclosed array. How do i do this? I've asked this before and got an answer but i can't find anything now.alias this on the underlying array
Nov 01 2013
On Friday, November 01, 2013 12:30:10 Gary Willoughby wrote:I have a class which contains an array as a core collection of data. I want to pass an instance of this class to a foreach loop and iterate through the enclosed array. How do i do this? I've asked this before and got an answer but i can't find anything now.In general, if you want to make something work with foreach, you either make it a range, or you give it an opSlice which returns a range (another alternative would be define opApply, but in general, code should be using the range primitives rather than opApply). Given that you're looking to iterate an array, and you presumably don't want the array to be consumed when iterating, the simplest would be to simply declare an opSlice on the class returns the array. e.g. class C { int[] foo; auto opSlice() { return foo; } } Then when you use the class in a foreach loop, it'll be sliced, and the return value of opSlice (the array in this case) will then be iterated over. - Jonathan M Davis
Nov 01 2013
On Friday, 1 November 2013 at 11:41:52 UTC, Jonathan M Davis wrote:On Friday, November 01, 2013 12:30:10 Gary Willoughby wrote:Hmmm, that's simpler too. ta.I have a class which contains an array as a core collection of data. I want to pass an instance of this class to a foreach loop and iterate through the enclosed array. How do i do this? I've asked this before and got an answer but i can't find anything now.In general, if you want to make something work with foreach, you either make it a range, or you give it an opSlice which returns a range (another alternative would be define opApply, but in general, code should be using the range primitives rather than opApply). Given that you're looking to iterate an array, and you presumably don't want the array to be consumed when iterating, the simplest would be to simply declare an opSlice on the class returns the array. e.g. class C { int[] foo; auto opSlice() { return foo; } } Then when you use the class in a foreach loop, it'll be sliced, and the return value of opSlice (the array in this case) will then be iterated over. - Jonathan M Davis
Nov 01 2013
On Friday, 1 November 2013 at 11:41:52 UTC, Jonathan M Davis wrote:On Friday, November 01, 2013 12:30:10 Gary Willoughby wrote:So we basically have 4 ways..? 1) popFront + front 2) opSlice 3) alias this 4) opApplyI have a class which contains an array as a core collection of data. I want to pass an instance of this class to a foreach loop and iterate through the enclosed array. How do i do this? I've asked this before and got an answer but i can't find anything now.In general, if you want to make something work with foreach, you either make it a range, or you give it an opSlice which returns a range (another alternative would be define opApply, but in general, code should be using the range primitives rather than opApply). Given that you're looking to iterate an array, and you presumably don't want the array to be consumed when iterating, the simplest would be to simply declare an opSlice on the class returns the array. e.g. class C { int[] foo; auto opSlice() { return foo; } } Then when you use the class in a foreach loop, it'll be sliced, and the return value of opSlice (the array in this case) will then be iterated over. - Jonathan M Davis
Nov 01 2013
On Friday, 1 November 2013 at 12:37:20 UTC, simendsjo wrote:2) opSlice 3) alias thisarguably these are the same in the context of the foreach loop. Both just provide direct access to the underlying array.
Nov 01 2013
01-Nov-2013 16:43, John Colvin пишет:On Friday, 1 November 2013 at 12:37:20 UTC, simendsjo wrote:No quite. I'd say alias this is frankly a bad idea to provide iteration. In any case returning naked underlying array is most surely short-sighted. In short we have 2 ways: 1) Ranges 2) opApply And a couple of extra things on _top_ of that to keep in mind: 1) Implicit conversion -> hence alias this to a range/opApply type works 2) opSlice is called on foreach aggregate if it's not a range or doesn't have opApply by itself. -- Dmitry Olshansky2) opSlice 3) alias thisarguably these are the same in the context of the foreach loop. Both just provide direct access to the underlying array.
Nov 01 2013
On 11/01/2013 08:32 AM, Dmitry Olshansky wrote:In short we have 2 ways: 1) Ranges 2) opApplyAs another shameless plug, those foreach methods are included in this chapter: http://ddili.org/ders/d.en/foreach_opapply.html Ali
Nov 01 2013
Ali:As another shameless plug, those foreach methods are included in thischapter:http://ddili.org/ders/d.en/foreach_opapply.htm<http://ddili.org/ders/d.en/foreach_opapply.html>OMG. I just grokked opApply. *epiphany moment* Thanks a lot, Ali! I tried to get it for *years*!
Nov 01 2013
On 11/01/2013 02:28 PM, Philippe Sigaud wrote:Ali:You made my day! :) I can't believe that the king of D templates learned something from my little chapter: https://github.com/PhilippeSigaud/D-templates-tutorial AliAs another shameless plug, those foreach methods are included in thischapter:http://ddili.org/ders/d.en/foreach_opapply.htm<http://ddili.org/ders/d.en/foreach_opapply.html>OMG. I just grokked opApply. *epiphany moment* Thanks a lot, Ali! I tried to get it for *years*!
Nov 01 2013
On Fri, Nov 1, 2013 at 11:15 PM, Ali =C3=87ehreli <acehreli yahoo.com> wrot= e:ub.com/PhilippeSigaud/D-templates-tutorial>You made my day! :) I can't believe that the king of D templates learnedsomething from my little chapter: https://github.com/**PhilippeSigaud/D-templates-**tutorial<https://gith=;-) You're too kind. I should dust this text somewhat.My understanding of D templates is now years behind the compiler (see the new `alias Name(T) =3D ...` syntax in 2.064. That's cool). Anyway, back to opApply. I should write something using it before I forget how to use it :) What did you use to generate your website, btw?
Nov 01 2013
On 11/01/2013 03:51 PM, Philippe Sigaud wrote:What did you use to generate your website, btw?It is a completely static web site (except the Google translate widget) that is produced by ddoc and a couple of Makefiles. The following is the Turkish readme file: https://code.google.com/p/ddili/source/browse/trunk/BENIOKU The tools required for the site are just dmd and GNU make. The free pdf generator prince is used for the pdf versions of the books. I just do make test to produce the light-weight versions of the pages during development and make to produce a single tar file to upload to the provider. Ali
Nov 01 2013
On Sat, Nov 2, 2013 at 12:08 AM, Ali =C3=87ehreli <acehreli yahoo.com> wrot= e:On 11/01/2013 03:51 PM, Philippe Sigaud wrote:This is something I'm looking for!What did you use to generate your website, btw?It is a completely static web site (except the Google translate widget) that is produced by ddoc and a couple of Makefiles.The following is the Turkish readme file: https://code.google.com/p/**ddili/source/browse/trunk/**BENIOKU<https:/=/code.google.com/p/ddili/source/browse/trunk/BENIOKU>The tools required for the site are just dmd and GNU make. The free pdf generator prince is used for the pdf versions of the books. I just do make test to produce the light-weight versions of the pages during development and make to produce a single tar file to upload to the provider.OK, that seems quite similar to what I want. Any limitation you hit with Ddoc? Where can I get a view of the internal .ddoc file you used for the macro definitions? I found the source docs for your book text, but not the one containing definitions. I'm planning to write a new text on D, and though I'm enamoured of Markdown, I'm willing to give Ddoc a chance, since Andrei pushes its usage. I see three limitations: - almost no one knows Ddoc. Markdown is simple and used by the entire planet. - no possibility to inject equations with Ddoc? - no possibility to put images? (I may be wrong on this one). The equation requirement might seem strange, but I'd like to write a ray-tracing tutorial in D, and ray-tracing 'theory' uses a lot of maths. Many markdown parser know LaTeX or MathML. Mayby with MathML and invoking HTML code inside Ddoc? Oh, and if you could find the time to translate the turkish page, that would be great!
Nov 02 2013
I will respond later with more information. Here is just the quick ones. :) On 11/02/2013 01:34 AM, Philippe Sigaud wrote:Where can I get a view of the internal .ddoc file you used for the macro definitions?They are the ones that end with .ddoc here: https://code.google.com/p/ddili/source/browse/trunk/#trunk%2Fsrc and additionally in each book directory, e.g.: https://code.google.com/p/ddili/source/browse/trunk/#trunk%2Fsrc%2Fders%2Fd.en (Beware that there is some "technical debt" there. ;) ) Some ddoc files are auto generated by scripts such as this one: https://code.google.com/p/ddili/source/browse/trunk/src/make_ders_macros.sh That one deals with the Prev and Next links to point at the neighboring pages.- no possibility to inject equations with Ddoc?I don't know anything about that. :(- no possibility to put images? (I may be wrong on this one).By the IMG macro for img links like this one uses: https://code.google.com/p/ddili/source/browse/trunk/src/ders/d.en/pdf_indir.d#10Oh, and if you could find the time to translate the turkish page, that would be great!To be continued... :) Ali
Nov 02 2013
On 11/02/2013 01:34 AM, Philippe Sigaud wrote:Any limitation you hit with Ddoc?One annoyance is with parentheses in code sections. Ddoc allows using ddoc macros even in code section and I like it because I can highlight parts of code by a macro. However, that means that unbalanced parentheses in code sections cause trouble. For example (if I remember correctly), even a string like "1) blah" in a code section causes formatting issues. So, one has to use the equivalents of the following macros:if you could find the time to translate the turkish page, that would be great!http://code.google.com/p/ddili/source/browse/trunk/README Ali
Nov 03 2013
On Mon, Nov 4, 2013 at 6:05 AM, Ali =C3=87ehreli <acehreli yahoo.com> wrote= :On 11/02/2013 01:34 AM, Philippe Sigaud wrote:cAny limitation you hit with Ddoc?One annoyance is with parentheses in code sections. Ddoc allows using ddo=macros even in code section and I like it because I can highlight parts o=fcode by a macro.Yeah, I saw that in your code. I didn't know that could be done.However, that means that unbalanced parentheses in code sections cause trouble. For example (if I remember correctly), even a string like "1) blah" in a code section causes formatting issues. So, one has to use the equivalents of the following macros:I saw these and wondered what they were for. Why comma and dollar?if you could find the time to translate the turkish page, that would be great!http://code.google.com/p/ddili/source/browse/trunk/READMEAh, thanks you, Ali. I now realize I got only a small part of it.
Nov 03 2013
On 11/03/2013 09:44 PM, Philippe Sigaud wrote:Actual comma inside a macro is taken as argument separator. From src/ders/d.en/operator_overloading.d: $(ROW3 slice to some elements, opSlice, collection[i$(VIRGUL) j]) (Note: VIRGUL is COMMA) If I used actual comma, " j]" would be taken as an unused extra parameter to ROW3. Dollar sign appears in dlang forum links. The only occurrence so far has been inside the Turkish translation of David Nadlinger's purity article, src/makale/saflik.d: $(LINK2 http://forum.dlang.org/thread/i7bp8o$(DOLAR)6po$(DOLAR)1 digitalmars.com, son değişikleri de getirmiş olan tartışma) Otherwise, the $ character would be parsed as the beginning of a macro. (Perhaps there is some sort of escaping but I just used the DOLAR macro. :-/) AliI saw these and wondered what they were for. Why comma and dollar?
Nov 03 2013
Am Fri, 01 Nov 2013 08:50:19 -0700 schrieb Ali =C3=87ehreli <acehreli yahoo.com>:On 11/01/2013 08:32 AM, Dmitry Olshansky wrote: =20I just saw a small mistake on that page: ".popFront() must move to the element that is one before the end (skips the back element)" popFront should be popBack.In short we have 2 ways: 1) Ranges 2) opApply=20 As another shameless plug, those foreach methods are included in this=20 chapter: =20 http://ddili.org/ders/d.en/foreach_opapply.html =20 Ali =20
Nov 03 2013
On Friday, November 01, 2013 19:32:35 Dmitry Olshansky wrote:01-Nov-2013 16:43, John Colvin пишет:Pretty much anytime that you're tempted to use alias this, you should reconsider. It _does_ have its uses, but implicit conversions tend to cause a lot problems - especially in generic code. And in this case, you can simply declare opSlice to return the underlying array, so there isn't really any benefit to using alias this other than the fact that it saves you a few characters (which isn't even vaguely worth the cost IMHO).On Friday, 1 November 2013 at 12:37:20 UTC, simendsjo wrote:No quite. I'd say alias this is frankly a bad idea to provide iteration. In any case returning naked underlying array is most surely short-sighted.2) opSlice 3) alias thisarguably these are the same in the context of the foreach loop. Both just provide direct access to the underlying array.In short we have 2 ways: 1) Ranges 2) opApply And a couple of extra things on _top_ of that to keep in mind: 1) Implicit conversion -> hence alias this to a range/opApply type works 2) opSlice is called on foreach aggregate if it's not a range or doesn't have opApply by itself.Yeah. Any user-defined type that's iterable via foreach uses either the range API or opApply. And in general, unless the range API isn't going to work for you, it's recommended to use the range API rather than opApply. opSlice comes into play when you want to iterate over something as a range without consuming it. The most common place to see opSlice would likely be on containers. - Jonathan M Davis
Nov 01 2013
On Friday, 1 November 2013 at 12:37:20 UTC, simendsjo wrote:On Friday, 1 November 2013 at 11:41:52 UTC, Jonathan M Davis wrote:How about having a nested struct implementing a range interface + a method ( property) returning it + alias this on the property? http://dpaste.dzfl.pl/230061d2On Friday, November 01, 2013 12:30:10 Gary Willoughby wrote:So we basically have 4 ways..? 1) popFront + front 2) opSlice 3) alias this 4) opApplyI have a class which contains an array as a core collection of data. I want to pass an instance of this class to a foreach loop and iterate through the enclosed array. How do i do this? I've asked this before and got an answer but i can't find anything now.In general, if you want to make something work with foreach, you either make it a range, or you give it an opSlice which returns a range (another alternative would be define opApply, but in general, code should be using the range primitives rather than opApply). Given that you're looking to iterate an array, and you presumably don't want the array to be consumed when iterating, the simplest would be to simply declare an opSlice on the class returns the array. e.g. class C { int[] foo; auto opSlice() { return foo; } } Then when you use the class in a foreach loop, it'll be sliced, and the return value of opSlice (the array in this case) will then be iterated over. - Jonathan M Davis
Nov 03 2013
On Sunday, November 03, 2013 14:28:13 Nicolas Sicard wrote:alias this should be avoided as much as possible. It's a bug magnet - especilaly when generic code is involved. And there's no reason to create a new range type in this case, because all that needs to be iterated over is the array. It makes by far the most sense to simply treat the class as a container, and overload opSlice so that it returns the array to be iterated over. If there was ever a need to do something more complicated than just iterate over the array, then a new range type could be returned from opSlice instead, but as it stands simply returning the array would be just fine. - Jonathan M DavisSo we basically have 4 ways..? 1) popFront + front 2) opSlice 3) alias this 4) opApplyHow about having a nested struct implementing a range interface + a method ( property) returning it + alias this on the property? http://dpaste.dzfl.pl/230061d2
Nov 05 2013