DIP 1019--Named Arguments Lite--Final Review

a11e99z black80 at bk.ru
Fri Aug 23 19:21:48 UTC 2019


On Friday, 23 August 2019 at 17:59:38 UTC, Andrei Alexandrescu 
wrote:
> On 8/23/19 1:42 PM, H. S. Teoh wrote:
>> On Fri, Aug 23, 2019 at 05:16:16PM +0000, bachmeier via 
>> Digitalmars-d wrote:
>>> On Friday, 23 August 2019 at 16:47: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);
>
> Exactly. It seems that for short lists (2-3 arguments) naming 
> would be a net negative (add notational overhead when there's 
> little possibility of a confusion to start with). Then, when 
> the list gets longer it actually makes matters _worse_ because 
> it disallows skipping of defaulted arguments.
>
> This can't go.

C# version (C# online REPL 
https://repl.it/repls/KnowingDeliriousLifecycles)
==============================
using System;
class MainClass {
   static void fun( int arg1, string arg2, double arg3, string 
def1 ="one", int def2 = 2) {
     Console.WriteLine( $"fun( {arg1}, {arg2}, {arg3}, {def1}, 
{def2} )");
   }
   public static void Main (string[] args) {
     // ok
     fun( arg3:3.14, arg2:"world", arg1:789);

     // Named arguments must appear after the positional arguments
     //fun( 123, arg2:"world", 2.72, def1:"hello" );

     // ok now
     fun( 123, arg2:"hello", arg3:9.8, def2:-2);

     // defaulted args can be skipped in any order
     fun( 123, "hello", 6e23, def1:"world");
     fun( 123, "hello", def2:-2, arg3:3e8);

     //The best overloaded method match for `MainClass.fun(int, 
string, double, string, int)' has some invalid arguments
     // Argument `#2' cannot convert `int' expression to type 
`string'
     //fun( 123, 735, def2:-2, arg2:"wat?");
   }
}
==============================
fun( 789, world, one, 456 )
fun( 123, world, hello, 2 )
fun( 123, hello, one, 456 )
fun( 123, hello, world, 2 )

> defaulted args = args that have default value
totally:
- named args follows by unnamed args, not vice versa
- unnamed args correspond to order of args in func-DEF.
   any of unnamed arg can be non-defaulted or defaulted arg in 
func-DEF.
   (as now)
- named arg must not be specified again if its already set as 
unnamed.
- u cannot skip defaulted args in unnamed list (as now)
   [what is not prohibited is allowed]:
     any defaulted args can be skipped when named list has begun



More information about the Digitalmars-d mailing list