Named parameters

Johannes Pfau via Digitalmars-d digitalmars-d at puremagic.com
Sat Jul 25 01:39:09 PDT 2015


Am Fri, 24 Jul 2015 14:04:17 -0700
schrieb Walter Bright <newshound2 at digitalmars.com>:

> On 7/24/2015 10:15 AM, tcak wrote:
> > How would this create a problem I can't see.
> 
> The question is what problem does it solve.

At least:
1 Better readable code with literal parameters
  auto x = window.addNewControl("Title", 20, 50, 100, 50, true);
  There are some domains which would benefit a lot from this (every
  OpenGL function call, most graphics/image libraries)

2 If you have many parameters with default values, you can keep default
  values for all other parameters and overwrite only one. This can
  sometimes be done with overloads, but what if you've got parameters
  of the same type:
  void foo(bool a = function1(x, y), bool b = function2(x, y), bool c =
      function1(x, y))
  foo(function1(x, y), function2(x, y), false); => foo(c = false)

3 One thing that's sometimes annoying in D is that it's not possible to
  have normal arguments after a variadic argument. Named parameters
  could solve that:
  void foo(A, T... args, B) => foo!(int, int, bool, B = bool)


Note that there are languages which allow named parameters but do not
allow parameter reordering. This still allows 1 and 3 but the
implementation is simpler.


More information about the Digitalmars-d mailing list