DIP 1030--Named Arguments--Community Review Round 1 Discussion

rb3 ryanblonna3 at gmail.com
Mon Feb 10 04:31:06 UTC 2020


On Monday, 10 February 2020 at 02:25:07 UTC, rikki cattermole 
wrote:
> On 10/02/2020 7:32 AM, Steven Schveighoffer wrote:
>> 
>> Not sure this could be doable in D. But for a main language 
>> that started out with significant naming of parameters 
>> (inheriting this from objective-C), it's a good place to look 
>> for inspiration here.
>
> Alternatively support implicit construction of structs as 
> parameters i.e.
>
> void foo(Nullable!int ms);
>
> foo(ms: 3);
>
> You have to do some mapping internally but it means even if you 
> deprecate a name, you can still keep the old ones.

This sounds like a good unification between struct initialization 
and named arguments.

So to "enable" named arguments, one would simply declare a struct 
like this:

struct BufferCreateInfo
{
     const(char)* type;
     size_t size;
}

and then use the struct type in a function argument list:

Buffer createBuffer(BufferCreateInfo info);

and call it like this:

auto buffer = createBuffer({ type: "BufferType", size: 16 }); // 
or createBuffer(type: "BufferType", size: 16) for syntax sugar

or if you want mixed named and non-named arguments:

Buffer createBuffer(BufferCreateInfo info, size_t howMany); // 
createBuffer(type: "BufferType", size: 16, 10);

The only change is allowing struct construction on a function 
parameter, then maybe take it a step further by eliminating the 
curly braces in function calls. But I don't know anything about 
compilers...


More information about the Digitalmars-d mailing list