Struct initializers as expressions

Chris Wright via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Dec 2 21:26:17 PST 2015


I can initialize a struct with named values:

---
struct Foo {
  int i, j, k, l, m, n;
}
Foo f = {k: 12};  // other fields get default initialization
---

I can initialize it with call syntax:

---
auto f = Foo(0, 0, 12, 0, 0, 0);  // works
---

I can use the latter as an expression:

---
void bar(Foo f) {}
bar(Foo(0, 0, 12, 0, 0, 0));  // just peachy
---

but not the latter:

---
void bar(Foo f) {}
bar({k: 12});
bar(Foo{k: 12});
---

Those both give:
Error: found '{' when expecting ','
Error: found ':' when expecting ','
Error: found '}' when expecting ','

This is slightly cruddy. I wanted to write

couchdb.queryView("byUsername", {key: "neia"});

But it looks like I have to use:

QueryOptions o = {key: "neia"};
couchdb.queryView("byUsername", o);

Not so good.

Ideally, I want something that lets you specify only the fields you care 
about and won't have to be modified (just recompiled) if I add more 
fields. I also want something inline.

Is there anything I can use here that's better than what I have?


More information about the Digitalmars-d-learn mailing list