digitalmars.D.learn - Returning constant / literal struct value (pod)
- Cecil Ward (11/11) Mar 09 2018 Can we return a literal struct value straight from a return
- H. S. Teoh (11/20) Mar 09 2018 The usual D idiom is to write:
Can we return a literal struct value straight from a return statement ? ie something like mystruct_t myfunc() { // ... blah return { field1: val1, field2: val2; }; } assuming that the return type is defined suitably, and the struct is just a C struct, plain-old-data (ie not a C++-like class). I've had to set up a throwaway const item, initialised, and then had to return that.
Mar 09 2018
On Fri, Mar 09, 2018 at 09:30:53PM +0000, Cecil Ward via Digitalmars-d-learn wrote:Can we return a literal struct value straight from a return statement? ie something like mystruct_t myfunc() { // ... blah return { field1: val1, field2: val2; }; } assuming that the return type is defined suitably, and the struct is just a C struct, plain-old-data (ie not a C++-like class).The usual D idiom is to write: mystruct_t myfunc() { return mystruct_t(val1, val2); } Unfortunately, AFAIK at this time there's no way to write field names as well, so you'll have to rely on the struct's field order or the ctor's parameter order. (But I could be wrong.) T -- Never step over a puddle, always step around it. Chances are that whatever made it is still dripping.
Mar 09 2018