DIP 88: Simple form of named parameters

Chris Wright via Digitalmars-d digitalmars-d at puremagic.com
Sat Jan 23 10:27:16 PST 2016


On Sat, 23 Jan 2016 15:19:03 +0100, Jacob Carlborg wrote:

> This is mostly to prevent ugly hacks like Flag [1].
> 
> http://wiki.dlang.org/DIP88

Please add proposals to http://wiki.dlang.org/List_of_DIP in the future. 
(I just did it for you.) There's a DIP category, but nobody includes a 
descriptive title in their wiki page names, so the auto-generated 
category name is kind of useless.

> [1] https://dlang.org/phobos/std_typecons.html#.Flag

"Rational" should be "Rationale".

One huge usecase for this is methods with many optional parameters. 
You've missed that. For instance, I wrote a method with six optional 
parameters recently. It's unusable without named parameters. I switched 
to a parameter struct, but it's still not that great.

I'd also add that this proposal doesn't affect UFCS.

What about overloading based on parameter names? The proposal doesn't 
even mention the idea, so there's no guessing whether you considered and 
rejected it or just didn't consider it.

> Any parameter that is supposed to be nameable at the call site needs to
> be explicitly declared as such. This is required because otherwise the
> parameters of all exist functions would become part of the API

I'll note that parameter names are already part of documentation, and 
they're already the only parameter documentation you're guaranteed to 
have.

The Dart devs decided that named parameters must be explicitly set out as 
named, and they cited the same reason. The C# devs, on the other hand, 
made every parameter a named parameter. I haven't heard of any explosions 
in C# land, and it's been five years.

Your proposal is closer to C#'s version than Dart's. In Dart, a named 
parameter cannot be called positionally:

  foo({a, b}) {}
  main() {
    // This works:
    foo(b: 1, a: 2);

    // Error: 0 positional arguments expected, 2 found
    foo(2, 1);
  }

In C#, a named parameter is only about the call site and has nothing to 
do with the function being called. A function you wrote for C# 1.1 and 
never modified can be called with named parameters in C# 4.0. You can 
still call these functions with positional-style parameters.

> Changing the order when using the named parameter syntax is not legal:

Why not?

Let's look at languages with named parameters. Python: reorderable. C#: 
reorderable. Dart: reorderable. Scala: reorderable. Ada: reorderable.

Given this trend, people will be confused if they can't reorder named 
parameters. The main benefit of not letting people reorder things is 
reducing the amount of work *you* need to do when creating this DIP (and 
the amount others need to do when implementing it).

To sum up, your proposal is approximately the same as adding a comment by 
each parameter saying which parameter it is. A bit nicer because it's got 
lighter syntax, a lot less usable because the function has to opt in.

Document how it's supposed to handle optional parameters and allow 
reordering, and then we're getting somewhere.


More information about the Digitalmars-d mailing list