DIP 1019--Named Arguments Lite--Final Review

Yuxuan Shui yshuiv7 at gmail.com
Fri Aug 23 23:46:43 UTC 2019


On Friday, 23 August 2019 at 18:35:26 UTC, bachmeier wrote:
> On Friday, 23 August 2019 at 17:42:14 UTC, H. S. Teoh wrote:
>
>> But this:
>>
>> 	foo(x: 4, y: 8, z: 9,
>> 		screen: scr1, widget: wg2,
>> 		options: opts, menu: menu3);
>>
>> is no worse than this:
>>
>> 	FooParams params;
>> 	params.x = 4;
>> 	params.y = 8;
>> 	params.z = 9;
>> 	params.screen = scr1;
>> 	params.widget = wg2;
>> 	params.options = opts;
>> 	params.menu = menu3;
>> 	foo(params);
>
> What that leads to is this:
>
> foo(x: 4, y: 8, z: 9,
> 	screen: scr1, widget: wg2,
> 	options: opts, menu: menu3);
> foo(x: 4, y: 8, z: 9,
> 	screen: scr2, widget: wg2,
> 	options: opts, menu: menu3);
> foo(x: 4, y: 8, z: 9,
> 	screen: scr1, widget: wg1,
> 	options: opts, menu: menu3);
>
> You've got a combination of extreme verbosity, hard-to-read 
> code, and a prolific bug generation machine.

How is that hard to read? Personally I think the FooParams 
version is more verbose and harder to read. Also the FooParams 
version forces default values on all parameters so when you 
forgot to specify one of the parameters, you won't get an error, 
which I consider to be very bad.

Also, you don't have to write the names if you don't want to.

> The answer to that is to start currying function arguments or 
> write a customized function that calls foo while holding fixed 
> certain parameters. It's extremely clear when you change a 
> single parameter in a struct. And nobody had to learn yet 
> another piece of syntax in order to read D code. The best 
> anyone can say is that this is not harmful in simple cases.

This syntax is so simple that I think it's definitely worthwhile




More information about the Digitalmars-d mailing list