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

Petar Petar
Thu May 14 12:03:13 UTC 2020


On Thursday, 14 May 2020 at 11:56:41 UTC, Petar Kirov 
[ZombineDev] wrote:
> On Thursday, 14 May 2020 at 05:19:21 UTC, Walter Bright wrote:
>> On 5/13/2020 12:31 PM, Steven Schveighoffer wrote:
>>> On 5/13/20 3:30 PM, Walter Bright wrote:
>>>> In D, we do:
>>>>     void copy(string, string destination);
>>> And how does the implementation of copy use that first 
>>> parameter?
>>
>> ----- test.di -----
>> void copy(string, string destination);
>>
>> ----- test.d ------
>> void copy(string src, string destination)
>> {
>>     ...
>> }
>
>
> Since .di files quite rare in D, compared to C and C++, can we 
> also support the following:
>
> ----- test.d ------
>
> // originally written as `void copy(string src, string dst)`:
> void copy(string source, string destination)
> {
>     ...
> }
>
> // `copy` function author decided to improve the function 
> signature,
> // by not using abbreviated names. To prevent breaking changes 
> they
> // add a deprecated declaration:
> deprecated("Parameters renamed: `src` -> `source` | `dst` -> 
> `destination`")
> void copy(string src, string dst);
>
> // However the author made a mistake and actually wrote:
> // void copy(string source, string distinasion)
> // Being extra careful about their users, the author adds 
> another
> // deprecated declaration:
> deprecate("`distinasion` was renamed to `destination` to 
> correct typo")
> void copy(string source, string distinasion);
>
> ----- old_time_user.d ------
> void main()
> {
>     // keeps working, though emits a deprecation message:
>     // Parameters renamed: `src` -> `source` | `dst` -> 
> `destination`
>     copy(src: "/some/path", dst: "/another/path");
> }
>
> ----- new_user.d ------
> void main()
> {
>     // works with no deprecation messages
>     copy(source: "/some/path", destination: "/another/path");
> }
>
> ----- conservative_user.d ------
> void main()
> {
>     // works without ambiguity error
>     copy("/some/path", "/another/path");
> }
>
>
> Since parameter names are not going to be part of the name 
> managing, I propose that the compiler merges function 
> declarations that produce the same mangled name in order to not 
> emit ambiguity errors.

BTW, the following compiles today:

--- library.d
void copy(string source, string destination) { }

deprecated("Parameters renamed: `src` -> `source` | `dst` -> 
`destination`")
void copy(string src, string dst);

deprecated("`distinasion` was renamed to `destination` to correct 
typos")
void copy(string source, string distinasion);

The problem is when you try to call the copy function:

--- main.d
import library;
void main()
{
     copy("/some/path", "/another/path"); // Error [1]
}

[1]:
/sandbox/main.d(5): Error: `library.copy` called with argument 
types `(string, string)` matches both:
/sandbox/library.d(1):     `library.copy(string source, string 
destination)`
and:
/sandbox/library.d(7):     `library.copy(string source, string 
distinasion)`


More information about the Digitalmars-d mailing list