DIP 1019--Named Arguments Lite--Community Review Round 2
Jonathan Marler
johnnymarler at gmail.com
Fri Jun 7 06:05:07 UTC 2019
On Thursday, 6 June 2019 at 20:04:15 UTC, Walter Bright wrote:
> Although this supports reordering, the real reason for naming
> is so one can
> have
> a function with a longish list of parameters, each with a
> reasonable default,
> and the user need only supply the arguments that matter for his
> use case. This
> is much more flexible than the current method of putting all
> the defaults at
> the
> end of the parameter list, and defaulting one means all the
> rest get defaulted.
>
> A secondary reason is (again) for a longish list of parameters,
> when the user
> finds himself counting parameters to ensure they line up, then
> named parameters
> can be most helpful.
It's also useful for
1. boolean flags
foo(true);
foo(log: true);
2. parameters with units
sleep(1);
sleep(seconds: 1);
3. parameters where the order matters but is not obvious
copy(a, b);
copy(src: a, dst: b);
Of course there are other ways to accomplish these such as using
abstract types like Flag and Duration to specify names and units
or establishing name conventions to indicate parameter ordering.
But named parameters also make these cases pretty convenient.
More information about the Digitalmars-d
mailing list