Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
August 22, 2014 How I can iterate data in structure | ||||
---|---|---|---|---|
| ||||
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 Re: How I can iterate data in structure | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | 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 Re: How I can iterate data in structure | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | >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 Re: How I can iterate data in structure | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | 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 Re: How I can iterate data in structure | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | 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 Re: How I can iterate data in structure | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz | 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 |
Copyright © 1999-2021 by the D Language Foundation