DIP 88: Simple form of named parameters

Marc Schütz via Digitalmars-d digitalmars-d at puremagic.com
Sat Jan 23 09:08:03 PST 2016


On Saturday, 23 January 2016 at 16:38:13 UTC, Jacob Carlborg 
wrote:
> void foo(int a:, string b:);
> void foo(string b:, int a:);
>
> void main()
> {
>     foo(a: 3, b: "asd");
> }
>
> What should happen in the above example? Error or which 
> function should be called?

I hadn't take this into consideration; indeed it would change the 
way overload resolution works, although it is still possible to 
handle this (in this case with an error, because it's ambiguous).

My thought is that for flags (which was your justification for 
this feature), you really wouldn't want to care about order, you 
just want to specify the flags you are interested in and let the 
others assume their default values.

For an example, consider this overload of `spawnProcess` [3]:

@trusted Pid spawnProcess(
     in char[][] args,
     File stdin = std.stdio.stdin,
     File stdout = std.stdio.stdout,
     File stderr = std.stdio.stderr,
     const string[string] env = null,
     Config config = Config.none,
     in char[] workDir = null
);

Let's say you want to specify the workdir, stdout and a config 
flag:

auto pid = spawnProcess(
     ["/usr/bin/grep", "-r.", "some string"],
     workDir: "~/Documents",
     stdout: fd,
     config: Config.retainStderr
);

If the arguments all must be specified in the right order, it's 
very hard to get them right without looking them up in the 
documentation. Besides, if I understand the proposal correctly, 
you'd actually have to specify all arguments up to the last one 
used, even though they already have default values.

> We already have ParameterIdentifierTuple [1]. Are you thinking 
> of something else?

ParameterIdentifierTuple doesn't distinguish between parameters 
that allow your syntax and those that don't.

>
> I'm not seeing how AliasSeq is related but this PR [2] adds 
> template parameter introspection traits.
>
> [1] 
> http://dlang.org/phobos/std_traits.html#ParameterIdentifierTuple
> [2] https://github.com/D-Programming-Language/dmd/pull/5201

This was part of my thoughts for variadic template params. 
Basically, the following should work:

int foo(int a:, int b:);
int bar(Args...)(Args args:) {
     return bar(args);
}
bar(b: 2, a: 1);

Currently, `args` is `(2, 1)`. With name parameters, it would 
become `(b: 2, a: 1)`, i.e. each tuple members gets an (optional) 
name. There would then need to be a trait for reading these names.

[3] http://dlang.org/phobos/std_process.html#.spawnProcess


More information about the Digitalmars-d mailing list