Opinions on iterating a struct to absorb the decoding of a CSV?
H. S. Teoh
hsteoh at qfbox.info
Thu Mar 28 17:32:04 UTC 2024
On Thu, Mar 28, 2024 at 05:23:39PM +0000, Andy Valencia via Digitalmars-d-learn wrote:
[...]
> auto t = T();
> foreach (i, ref val; t.tupleof) {
> static if (is(typeof(val) == int)) {
> val = this.get_int();
> } else {
> val = this.get_str();
> }
> }
> return t;
>
> So you cue off the type of the struct field, and decode the next CSV
> field, and put the value into the new struct.
>
> Is there a cleaner way to do this? This _does_ work, and gives me
> very compact code.
This is pretty clean, and is a good example of DbI. I use the same
method in my fastcsv experimental module to transcribe csv to an array
of structs:
https://github.com/quickfur/fastcsv
T
--
Today's society is one of specialization: as you grow, you learn more and more about less and less. Eventually, you know everything about nothing.
More information about the Digitalmars-d-learn
mailing list