Thread overview
Designated initializers to function argument
Jul 11, 2023
Ki Rill
Jul 11, 2023
Ki Rill
Jul 28
IchorDev
Jul 28
bachmeier
Jul 28
IchorDev
Jul 28
bachmeier
July 11, 2023

In C I can do the following:

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?

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...

July 11, 2023

On Tuesday, 11 July 2023 at 15:16:54 UTC, Ki Rill wrote:

>

// 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).

July 11, 2023

On 7/11/23 11:22 AM, Ki Rill wrote:

>

On Tuesday, 11 July 2023 at 15:16:54 UTC, Ki Rill wrote:

>

// 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

July 28

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?

July 28

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

July 28

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 was approved long ago. It was waiting for an implementation.

July 28

On Friday, 28 July 2023 at 17:04:33 UTC, bachmeier wrote:

>

The DIP 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...?

July 28

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 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.

July 29

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.