On Jun 14, 11 20:34, Michel Fortin wrote:
On 2011-06-13 21:36:01 -0400, so <s...@so.so> said:

IMO named arguments in D at least should do:

- Reordering (since we got default parameters, even better)

- It is enabled only if we have access to the function declaration.

- In a function call we either use named arguments for all the
non-default arguments or call it with the usual syntax. No hybrid
stuff, no confusion.

fun(int a, int b, int c=3)

fun(1, 2, 3) // fine - current
fun(1, 2) // fine - current

fun(a:1, b:3, c:5) // fine
fun(a:1, b:3) // fine
fun(b:1, c:3, a:5) // fine
fun(b:1, a:3) // fine
fun(b:1) // error
fun(c:1) // error
fun(2, b:1) // error
fun(2, c:1) // error
fun(2, c:2, 3) // error

There's much more to named arguments as it first appears. Have you
thought about:

1. variadic arguments?
2. named template arguments?

    partition!(ss:SwapStrategy.stable,
               predicate:"a>0")
              (r:[1,2,3,4]);

Variadic is a real problem though. IIRC C# doesn't allow it.

3. template variadic arguments (tuples) and how they expand as function
parameters?

Also, with reordering, if you have two overloaded functions of this form:

void fun(int a, string b) {}
void fun(string b, int a) {}

which one does this calls:

fun(a: 1, b: "hello");

? Does the call become ambiguous when using named arguments? It wouldn't
be ambiguous if reordering wasn't allowed. What does C# does with this?



Looks like the last one get the precedence. At least for Mono (see http://ideone.com/REXC0). MSDN says

============================================
Use of named and optional arguments affects overload resolution in the following ways:

- A method, indexer, or constructor is a candidate for execution if each of its parameters either is optional or corresponds, by name or by position, to a single argument in the calling statement, and that argument can be converted to the type of the parameter.

- If more than one candidate is found, overload resolution rules for preferred conversions are applied to the arguments that are explicitly specified. Omitted arguments for optional parameters are ignored.

- If two candidates are judged to be equally good, preference goes to a candidate that does not have optional parameters for which arguments were omitted in the call. This is a consequence of a general preference in overload resolution for candidates that have fewer parameters.
============================================

It doesn't cover your case. Of course this is bad. But this isn't much different from another solved problem...

----------------------------------------
void f(float s){}
void f(double s){}

void main() {
    f(3);
}
----------------------------------------
x.d(5): Error: function x.f called with argument types:
        ((int))
matches both:
        x.f(float s)
and:
        x.f(double s)
----------------------------------------

We could do the same for named arguments.

----------------------------------------
void fun(int a, string b) {}
void fun(string b, int a) {}

void main() {
    fun(a: 1, b: "hello");
}
----------------------------------------
x.d(5): Error: function x.fun called with argument names and types:
        ((int a, string b))
matches both:
        x.f(int a, string b)
and:
        x.f(string b, int a)
----------------------------------------

Reply via email to