digitalmars.D.learn - How I can iterate data in structure
- Suliman (21/21) Aug 22 2014 void main()
- bearophile (17/38) Aug 22 2014 Be careful to make ConfigStruct a static struct.
- Suliman (5/6) Aug 22 2014 Why I should here specify type of iterable element, but not first
- bearophile (5/11) Aug 22 2014 I don't understand your question. In my code I have not specified
- "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> (10/16) Aug 22 2014 You mustn't, because your struct could have fields of different
- Gary Willoughby (4/23) Aug 22 2014 Or you could implement opApply or range primitives in the struct.
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; }
Aug 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
Aug 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) ?
Aug 22 2014
Suliman:I don't understand your question. In my code I have not specified types in the foreach. Bye, bearophileforeach (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) ?
Aug 22 2014
On Friday, 22 August 2014 at 08:44:51 UTC, Suliman wrote: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); }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) ?
Aug 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: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.htmlYou 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); }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) ?
Aug 22 2014