DIP 1030--Named Arguments--Community Review Round 1 Discussion
rikki cattermole
rikki at cattermole.co.nz
Mon Feb 10 04:36:17 UTC 2020
On 10/02/2020 5:31 PM, rb3 wrote:
> 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...
That is not what I suggested.
What I suggested was given a single argument, automatically
construct+call the constructor on a struct as defined in a parameter.
It is a 1 to 1 rewrite and would be very simple to do.
More information about the Digitalmars-d
mailing list