On 04/02/18 13:33, Marcel Mueller wrote:
> On 04.02.18 12.38, David Brown wrote:
>> Named arguments would be a huge benefit to C++, IMHO.
>
> Ack.
>
> But there are side effects because the name of the arguments become part
> of the public API.
I see that as a bonus, and a good thing. Pick good names for your
arguments that say what they do - and use them for the declarations
/and/ definitions. I would be absolutely fine with the idea that these
are required to match, that mismatches would be a compilation (or
linking) error, and even that they are part of name mangling.
> First of all programmers might not be aware of that and rename their
> args due to of refactoring. OK, one could refactor only the names in the
> /implementation/.
> Furthermore consistency aspects may arise when overriding functions with
> different argument names. Restrictions would be helpful but they would
> break existing code.
>
Extra limitations on argument names would indeed be a compatibility
problem. The solution would simply be to say that only functions where
the identifiers matched up could be used for named argument calls.
>> They would
>> clearly be useful in cases where you have lots of parameters, perhaps
>> with many default parameters. But they would also be useful when you
>> have only a few parameters.
>
> I would also add overload resolution: named parameters could
> disambiguate calls.
> It might even be allowed to have two overloads with identical types but
> distinct semantics, e.g. substring(int start, int length) vs.
> substring(int start, int end). Unfortunately the latter requires ABI
> changes.
>
It does not need any ABI calls when you have named arguments. The
compiler has the original declaration, which gives the order required
for the definition (which must match). If you write "substring(.start =
2, .length = 10);" and then "substring(.length = 10, .start = 2);", then
the compiler will match up the parameter order to fit the declaration
(and therefore the definition).
This is the way it works in many other programming languages - named
arguments is standard practice in modern language design, and C++ stands
out by missing this feature.