Please add named-parameter support at call site

Topics: Language Specification
Oct 12, 2012 at 9:57 PM
Edited Oct 12, 2012 at 9:57 PM

See:

function doSomething(name:string, age?:number, height?:number) {
}

doSomething("James", height:5);

// Generated code:

doSomething("James", undefined, 5);
Oct 13, 2012 at 2:47 AM
Edited Oct 13, 2012 at 2:48 AM

It's not quite the same, but object literals give similar results and are already supported in TypeScript and Javascript.

 

function doSomething(name: string, otherData? : { age?:number; height?:number}) {

}

doSomething("James", { height: 5 });
Oct 13, 2012 at 4:16 AM
Alexjs wrote:

It's not quite the same, but object literals give similar results and are already supported in TypeScript and Javascript.

 

function doSomething(name: string, otherData? : { age?:number; height?:number}) {

}

doSomething("James", { height: 5 });

Indeed – I have used this syntax in CoffeeScript too, but as you said, it's not quite the same.  Having first-class support would encourage it's use as a common pattern.  It makes code far more readable, particularly with booleans.  I use it extensively in C#, regardless of whether parameters are optional or not.

In the following example, if you didn't know the API you would likely have a difficulty understanding the following code without looking at documentation:

 

deleteDirectory("/this/path", true, false);

 

With named parameters you can answer it comfortably:

 

deleteDirectory("/this/path", recursive:true, throwExceptions:false);

 

I would love to see this added to the spec.

Cheers,

Stu

Coordinator
Oct 15, 2012 at 3:22 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.