www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Anything like C99 compound literals in D?

reply John Burton <john.burton jbmail.com> writes:
This is just an example...

In windows WNDCLASS is a struct and RegisterClass is a function 
that takes a pointer to an instance of that class.

In C99 I can use "compound literals" to write this and have it 
construct an instance of that struct on the stack, set the values 
I've initialized, and default the rest of the struct members to 
zero. (There are many other members I just want to set to zero)

RegisterClass(&(WNDCLASS) {
     .hInstance = GetModuleHandle(NULL),
     .lpszClassName = "Hello",
     .lpfnWndProc = DefWindowProc
});

I find this very handy in my own code too. The syntax is very 
nice.
Is there any similar feature in D? (or a clean way to do this 
kind of thing)
Feb 09 2018
next sibling parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Friday, 9 February 2018 at 12:14:52 UTC, John Burton wrote:
 This is just an example...

 In windows WNDCLASS is a struct and RegisterClass is a function 
 that takes a pointer to an instance of that class.

 In C99 I can use "compound literals" to write this and have it 
 construct an instance of that struct on the stack, set the 
 values I've initialized, and default the rest of the struct 
 members to zero. (There are many other members I just want to 
 set to zero)

 RegisterClass(&(WNDCLASS) {
     .hInstance = GetModuleHandle(NULL),
     .lpszClassName = "Hello",
     .lpfnWndProc = DefWindowProc
 });

 I find this very handy in my own code too. The syntax is very 
 nice.
 Is there any similar feature in D? (or a clean way to do this 
 kind of thing)
[This should be posted to the learn forums].
Feb 09 2018
prev sibling parent Seb <seb wilzba.ch> writes:
On Friday, 9 February 2018 at 12:14:52 UTC, John Burton wrote:
 This is just an example...

 In windows WNDCLASS is a struct and RegisterClass is a function 
 that takes a pointer to an instance of that class.

 In C99 I can use "compound literals" to write this and have it 
 construct an instance of that struct on the stack, set the 
 values I've initialized, and default the rest of the struct 
 members to zero. (There are many other members I just want to 
 set to zero)

 RegisterClass(&(WNDCLASS) {
     .hInstance = GetModuleHandle(NULL),
     .lpszClassName = "Hello",
     .lpfnWndProc = DefWindowProc
 });

 I find this very handy in my own code too. The syntax is very 
 nice.
 Is there any similar feature in D? (or a clean way to do this 
 kind of thing)
Selective struct initialization works in D: MyStruct s = { a: "aa", b: 1, } Though not in-place, but there's already a DIP in the queue: https://github.com/dlang/DIPs/pull/71 We just need to get the DIP in its final shape (and maybe super-charge it with an actual DMD PR).
Feb 09 2018