Returning constant / literal struct value (pod)

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Mar 9 22:15:29 UTC 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.


More information about the Digitalmars-d-learn mailing list