newbie question: Can D do this?

Christophe travert at phare.normalesup.org
Wed Dec 21 04:13:12 PST 2011


Timon Gehr , dans le message (digitalmars.D.learn:31142), a écrit :
> On 12/20/2011 03:18 PM, clk wrote:
>> Thank you for your quick replies. I'm impressed by the helpfulness and
>> dedication of the D community!
>> Here's another one. Is there a way to pass arguments to functions by
>> keyword as in the calls to f and g below?
>>
>> void f(int a = 0, int b = 1) {}
>> void g(int a) {}
>>
>> void main() {
>> f(b = 1, a = 0); // compile error
>> g(a = 0); // also compile error
>> }
>>
>>
> 
> No, there are no named arguments in D. Having them would sometimes be 
> useful,

> but the drawback is that the parameter names become part of the 
> public interface.

Well, that's precisely the point. And it is a drawback if parameters are 
systematically names, but not if it is triggered only on demand.

Example :

void foo(int a, int b:, int c:);

void main() {
	foo(1, 2, 3);
	foo(1, c: 3, b: 2;
	foo(a: 1, b: 2, c: 3); // error : a is not a named parameter.
}

In the example, ":" is used to make a named parameter to recall the use 
when you call the function.



More information about the Digitalmars-d-learn mailing list