Thread overview
How I can iterate data in structure
Aug 22, 2014
Suliman
Aug 22, 2014
bearophile
Aug 22, 2014
Suliman
Aug 22, 2014
bearophile
Aug 22, 2014
Marc Schütz
Aug 22, 2014
Gary Willoughby
August 22, 2014
void main()
{
	
	auto result = readconfig();

	foreach (_; result)
	{
		// I want to iterate result that I got from structure.
	}
}

auto readconfig()
{
	struct ConfigStruct
	{
		string key1;
		string key2;
	}

	ConfigStruct confstruct = ConfigStruct();
	confstruct.key1="Ivan";
	confstruct.key2="admin";

	return confstruct;

}
August 22, 2014
Suliman:

> void main()
> {
> 	
> 	auto result = readconfig();
>
> 	foreach (_; result)
> 	{
> 		// I want to iterate result that I got from structure.
> 	}
> }
>
> auto readconfig()
> {
> 	struct ConfigStruct
> 	{
> 		string key1;
> 		string key2;
> 	}
>
> 	ConfigStruct confstruct = ConfigStruct();
> 	confstruct.key1="Ivan";
> 	confstruct.key2="admin";
>
> 	return confstruct;
>
> }

Be careful to make ConfigStruct a static struct.

auto readconfig() {
    static struct ConfigStruct {
        string key1, key2;
    }

    return ConfigStruct("Ivan", "admin");
}

void main() {
    import std.stdio;
    auto result = readconfig();

    foreach (field; result.tupleof) {
        writeln(field);
    }
}

Bye,
bearophile
August 22, 2014
>foreach (field; result.tupleof)

Why I should here specify type of iterable element, but not first element that I use for iteration?

I mean:
foreach (_some_type_possible_enum_ field; result)

?
August 22, 2014
Suliman:

>>foreach (field; result.tupleof)
>
> Why I should here specify type of iterable element, but not first element that I use for iteration?
>
> I mean:
> foreach (_some_type_possible_enum_ field; result)
>
> ?

I don't understand your question. In my code I have not specified types in the foreach.

Bye,
bearophile
August 22, 2014
On Friday, 22 August 2014 at 08:44:51 UTC, Suliman wrote:
>>foreach (field; result.tupleof)
>
> Why I should here specify type of iterable element, but not first element that I use for iteration?
>
> I mean:
> foreach (_some_type_possible_enum_ field; result)
>
> ?

You mustn't, because your struct could have fields of different types. When you `foreach()` over a tuple, the compiler unrolls the loop body, which allows it to use a (potentially) different type on each iteration.

If you don't want this, and all the fields have the same type, you can iterate over an array made from the fields:

    foreach (field; [result.tupleof]) {
        writeln(field);
    }
August 22, 2014
On Friday, 22 August 2014 at 10:44:31 UTC, Marc Schütz wrote:
> On Friday, 22 August 2014 at 08:44:51 UTC, Suliman wrote:
>>>foreach (field; result.tupleof)
>>
>> Why I should here specify type of iterable element, but not first element that I use for iteration?
>>
>> I mean:
>> foreach (_some_type_possible_enum_ field; result)
>>
>> ?
>
> You mustn't, because your struct could have fields of different types. When you `foreach()` over a tuple, the compiler unrolls the loop body, which allows it to use a (potentially) different type on each iteration.
>
> If you don't want this, and all the fields have the same type, you can iterate over an array made from the fields:
>
>     foreach (field; [result.tupleof]) {
>         writeln(field);
>     }

Or you could implement opApply or range primitives in the struct.

http://ddili.org/ders/d.en/foreach_opapply.html
http://dlang.org/phobos/std_range.html