www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Designated initializers to function argument

reply Ki Rill <rill.ki yahoo.com> writes:
In C I can do the following:
```C
void apply(struct Appearance a) {...}

// usage
apply((struct Appearance) {
     .color = BLACK,
     .strokeWidth = 4
});
```

Can I do the same in D without creating a struct variable 
separately?
```D
void apply(Appearance a) {...}

// currently accepts
Appearance a = { color: BLACK, strokeWidth: 4 };
apply(a);

// would like this
apply({ color: BLACK, strokeWidth: 4 });

// or this
apply(Appearance(color: BLACK, strokeWidth: 4)); // other fields 
are default initialized: strokeOpacity, fillOpacity, etc...

```
Jul 11 2023
parent reply Ki Rill <rill.ki yahoo.com> writes:
On Tuesday, 11 July 2023 at 15:16:54 UTC, Ki Rill wrote:
 ```D
 
 // or this
 apply(Appearance(color: BLACK, strokeWidth: 4)); // other 
 fields are default initialized: strokeOpacity, fillOpacity, 
 etc...

 ```
Ok, this works with DMD v2.103, but does not work with an older version (I had ldc2 installed based on DMD v2.98).
Jul 11 2023
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 7/11/23 11:22 AM, Ki Rill wrote:
 On Tuesday, 11 July 2023 at 15:16:54 UTC, Ki Rill wrote:
 ```D

 // or this
 apply(Appearance(color: BLACK, strokeWidth: 4)); // other fields are 
 default initialized: strokeOpacity, fillOpacity, etc...

 ```
Ok, this works with DMD v2.103, but does not work with an older version (I had ldc2 installed based on DMD v2.98).
Yes, I was going to reply that v 2.103 has added (stealthily) named parameters *as a partial implementation*. Included in this is struct initializers, and constructors and functions that are *not* templates. If you are willing to use DMD 2.103 and above, you should be good. -Steve
Jul 11 2023
parent reply IchorDev <zxinsworld gmail.com> writes:
On Tuesday, 11 July 2023 at 17:43:43 UTC, Steven Schveighoffer 
wrote:
 On 7/11/23 11:22 AM, Ki Rill wrote:
 On Tuesday, 11 July 2023 at 15:16:54 UTC, Ki Rill wrote:
 apply(Appearance(color: BLACK, strokeWidth: 4)); // other 
 fields are default initialized: strokeOpacity, fillOpacity,
Yes, I was going to reply that v 2.103 has added (stealthily) named parameters *as a partial implementation*. Included in this is struct initializers, and constructors and functions that are *not* templates. If you are willing to use DMD 2.103 and above, you should be good. -Steve
N-no way?! The spec makes no mention of them, is it really safe to use them yet?
Jul 28 2023
next sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 7/28/23 3:35 AM, IchorDev wrote:
 On Tuesday, 11 July 2023 at 17:43:43 UTC, Steven Schveighoffer wrote:
 On 7/11/23 11:22 AM, Ki Rill wrote:
 On Tuesday, 11 July 2023 at 15:16:54 UTC, Ki Rill wrote:
 apply(Appearance(color: BLACK, strokeWidth: 4)); // other fields are 
 default initialized: strokeOpacity, fillOpacity,
Yes, I was going to reply that v 2.103 has added (stealthily) named parameters *as a partial implementation*. Included in this is struct initializers, and constructors and functions that are *not* templates. If you are willing to use DMD 2.103 and above, you should be good.
N-no way?! The spec makes no mention of them, is it really safe to use them yet?
It isn't going away. I would wait a bit for libraries, because you don't want to force your users to require such a recent version of the compiler. Using it in an executable is fine. -Steve
Jul 28 2023
prev sibling parent reply bachmeier <no spam.net> writes:
On Friday, 28 July 2023 at 07:35:00 UTC, IchorDev wrote:
 On Tuesday, 11 July 2023 at 17:43:43 UTC, Steven Schveighoffer 
 wrote:
 On 7/11/23 11:22 AM, Ki Rill wrote:
 On Tuesday, 11 July 2023 at 15:16:54 UTC, Ki Rill wrote:
 apply(Appearance(color: BLACK, strokeWidth: 4)); // other 
 fields are default initialized: strokeOpacity, fillOpacity,
Yes, I was going to reply that v 2.103 has added (stealthily) named parameters *as a partial implementation*. Included in this is struct initializers, and constructors and functions that are *not* templates. If you are willing to use DMD 2.103 and above, you should be good. -Steve
N-no way?! The spec makes no mention of them, is it really safe to use them yet?
[The DIP](https://github.com/dlang/DIPs/blob/master/DIPs/accepted/DIP1030.md) was approved long ago. It was waiting for an implementation.
Jul 28 2023
parent reply IchorDev <zxinsworld gmail.com> writes:
On Friday, 28 July 2023 at 17:04:33 UTC, bachmeier wrote:
 [The 
 DIP](https://github.com/dlang/DIPs/blob/master/DIPs/accepted/DIP1030.md) was
approved long ago. It was waiting for an implementation.
No shit, it felt like an eternity. But it's still not in the spec...?
Jul 28 2023
parent reply bachmeier <no spam.net> writes:
On Friday, 28 July 2023 at 17:07:37 UTC, IchorDev wrote:
 On Friday, 28 July 2023 at 17:04:33 UTC, bachmeier wrote:
 [The 
 DIP](https://github.com/dlang/DIPs/blob/master/DIPs/accepted/DIP1030.md) was
approved long ago. It was waiting for an implementation.
No shit, it felt like an eternity. But it's still not in the spec...?
I'd expect it to appear in the spec after there's a real release. This is the first I've heard of it being supported, and it sounds like it's incomplete.
Jul 28 2023
parent Mike Parker <aldacron gmail.com> writes:
On Friday, 28 July 2023 at 21:07:47 UTC, bachmeier wrote:
 On Friday, 28 July 2023 at 17:07:37 UTC, IchorDev wrote:
 No shit, it felt like an eternity. But it's still not in the 
 spec...?
I'd expect it to appear in the spec after there's a real release. This is the first I've heard of it being supported, and it sounds like it's incomplete.
https://github.com/orgs/dlang/projects/19 Dennis has been working on this in pieces rather than all at once, as it required modification to multiple parts of the compiler.
Jul 28 2023