On Saturday, 3 February 2024 at 02:20:13 UTC, Paul Backus wrote:
On Friday, 2 February 2024 at 23:25:37 UTC, Chris Katko wrote:
The auto solution won't work for a struct however which I'm
using:
```D
struct procTable{ //contains all the fields inside a file I'm
parsing
uint time;
int priority;
string name;
// etc
}
```
Maybe you can use `typeof` in that case?
```d
procTable pt;
pt.time = to!(typeof(pt.time))(data[1]);
// etc
```
...although I guess then you're repeating the field name, which
isn't great either.
```d
struct ProcTable2 {
uint time;
int priority;
string name;
this (string [this.tupleof.length] initializers)
{
import std.conv;
static foreach (i, field; this.tupleof)
field = initializers [i].to!(typeof (field));
}
}
unittest {
string [3] initializers = ["100", "-5", "foobar"];
auto pt2 = ProcTable2 (initializers);
with (pt2) {
assert(time == 100);
assert(priority == -5);
assert(name == "foobar");
}
}
```