Re: Returning constant / literal struct value (pod)

2018-03-09 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Mar 09, 2018 at 09:30:53PM +, 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.


Returning constant / literal struct value (pod)

2018-03-09 Thread Cecil Ward via Digitalmars-d-learn
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.