How to iterate using foreach on a class?

evilrat evilrat666 at gmail.com
Fri Nov 1 05:05:37 PDT 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


More information about the Digitalmars-d-learn mailing list