zip with fieldTuple

John via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jun 6 15:16:34 PDT 2014


So let's say I'm trying to create a really simple ORM. I have a 
struct:

struct foo {
	int a;
	float b;
}

I can iterate over the struct elements with the traits 
FieldTypeTuple!foo, I can iterate over the the string that 
represents the elements I want to shove in the struct, but when I 
try to loop over *both* of these at the same time with zip(...) I 
get an error. Code:

void main() {
	string test = "1,2.0";
     foreach (t, value; zip(FieldTypeTuple!foo, test.split(","))) {
     	writeln(to!t(value));
     }
}

Error:

src\orm.d(13): Error: template std.range.zip does not match any 
function template declaration
C:\D\dmd2\windows\bin\..\..\src\phobos\std\range.d(3808): Error: 
template std.range.zip cannot deduce template function from 
argument types !()((int, float),string[])

I get what the error message is saying, but I have no idea how to 
fix the code to do what I want. I tried to look up what the 
FieldTypeTuple actually returns but it's calling a method on the 
generic type T called tupleOf(), which I can't seem to find (in 
that file or as a general function on object). I'm not sure if 
it's actually a range? I assumed it would be a range of some 
kind, and each of the elements would have a supertype of 
something like 'type' since that's what they are. It could infer 
that now you have two ranges, one of 'type' and one of 'string'.

If I'm able to foreach over two things, shouldn't I be able to 
foreach over the paired ranges with zip? It seems so simple...


More information about the Digitalmars-d-learn mailing list