Jump to page: 1 2 3
Thread overview
How to iterate using foreach on a class?
Nov 01, 2013
Gary Willoughby
Nov 01, 2013
simendsjo
Nov 01, 2013
Gary Willoughby
Nov 01, 2013
Gary Willoughby
Nov 01, 2013
evilrat
Nov 01, 2013
Jonathan M Davis
Nov 01, 2013
Gary Willoughby
Nov 01, 2013
simendsjo
Nov 01, 2013
John Colvin
Nov 01, 2013
Dmitry Olshansky
Nov 01, 2013
Ali Çehreli
Nov 01, 2013
Philippe Sigaud
Nov 01, 2013
Ali Çehreli
Nov 01, 2013
Philippe Sigaud
[OT] Generating ddili.org with ddoc (was: Re: How to iterate using foreach on a class?)
Nov 01, 2013
Ali Çehreli
Nov 02, 2013
Philippe Sigaud
Re: [OT] Generating ddili.org with ddoc
Nov 02, 2013
Ali Çehreli
Re: [OT] Generating ddili.org with ddoc
Nov 04, 2013
Ali Çehreli
Nov 04, 2013
Philippe Sigaud
Nov 04, 2013
Ali Çehreli
Nov 03, 2013
Johannes Pfau
Nov 01, 2013
Jonathan M Davis
Nov 03, 2013
Nicolas Sicard
Nov 06, 2013
Jonathan M Davis
November 01, 2013
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.
November 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
November 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:
>> 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

Ah that's it, opApply! ta.
November 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:
>> 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

Alias this?

November 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
November 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:
>> 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

Hmmm, that's simpler too. ta.
November 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:
>> 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
>
> Alias this?

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/struct
November 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:
>> 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

So we basically have 4 ways..?
1) popFront + front
2) opSlice
3) alias this
4) opApply
November 01, 2013
On Friday, 1 November 2013 at 12:37:20 UTC, simendsjo wrote:
> 2) opSlice
> 3) alias this

arguably these are the same in the context of the foreach loop. Both just provide direct access to the underlying array.
November 01, 2013
01-Nov-2013 16:43, John Colvin пишет:
> On Friday, 1 November 2013 at 12:37:20 UTC, simendsjo wrote:
>> 2) opSlice
>> 3) alias this
>
> arguably these are the same in the context of the foreach loop. Both
> just provide direct access to the underlying array.

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 Olshansky
« First   ‹ Prev
1 2 3