Thread overview | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
November 01, 2013 How to iterate using foreach on a class? | ||||
---|---|---|---|---|
| ||||
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 Re: How to iterate using foreach on a class? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | 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 Re: How to iterate using foreach on a class? | ||||
---|---|---|---|---|
| ||||
Posted in reply to simendsjo | 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 Re: How to iterate using foreach on a class? | ||||
---|---|---|---|---|
| ||||
Posted in reply to simendsjo | 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 Re: How to iterate using foreach on a class? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | 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 Re: How to iterate using foreach on a class? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | 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 Re: How to iterate using foreach on a class? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | 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 Re: How to iterate using foreach on a class? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | 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 Re: How to iterate using foreach on a class? | ||||
---|---|---|---|---|
| ||||
Posted in reply to simendsjo | 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 Re: How to iterate using foreach on a class? | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | 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 |
Copyright © 1999-2021 by the D Language Foundation