Discussion Thread: DIP 1030--Named Arguments--Final Review

Steven Schveighoffer schveiguy at gmail.com
Wed May 13 13:56:26 UTC 2020


On 5/13/20 9:15 AM, Paul Backus wrote:
> On Wednesday, 13 May 2020 at 07:55:33 UTC, Jacob Carlborg wrote:
>> Regarding renaming parameters will break the API. Swift supports 
>> giving a different name which are used locally:
>>
>> func copy(_ source: String, to destination: String)
>>
>> Should be called like this:
>>
>> copy("foo", to: "bar")
>>
>> `_` indicates that the argument can not be named when calling the 
>> function. `to` is the name that is used when calling the function. 
>> `source` and `destination` are the names used locally in the 
>> implementation of the function.
>>
>> This allows to rename a parameter (the local name) without breaking 
>> the API.
> 
> Worth noting that this can also be done in D using local alias 
> declarations:
> 
> void copy(const(char)[] _, char[] to) {
>      alias source = _;
>      alias destination = to;
>      // etc.
> }
> 
> Granted, the D compiler will not actually stop anyone from giving the 
> first argument by name, but it's hard to imagine why anyone would want to.

Note that in Swift, _ means "unnamed parameter", it's not an actual 
name. So we can't really do what it can do:

copy(_ source: String, _ destination: String)

could be called like:

copy("foo", "bar")

The equivalent in D would have multiple parameter names as _ which isn't 
allowed.

Hm... an interesting proposition, we could provide a way to disallow 
using names if the name starts with something like _:

void foo(int _x)
{
    alias x = _x;
    ...
}

foo(_x: 1); // Error
foo(1); // OK

It might be better than having to use .di files to prevent 
parameter-name based calling.

-Steve


More information about the Digitalmars-d mailing list