Structs and Variadic Functions (Keyword arguments)

Bill Baxter dnewsgroup at billbaxter.com
Fri Dec 22 02:14:31 PST 2006


Reiner Pope wrote:
> Xinok wrote:
>> I think combining structs with variadic functions can introduce some new
>> interesting possibilities.
>>
>> struct SS{
>>     int a, b, c;
>> }
>> static SS obj = {10, 20, 30}; // Full initalization
>> static SS obj = {a : 15, c : 35}; // Partial initalization
>>
>>
>> Apply this to a variadic function:
>> void func(SS arg ...){
>>     writefln(arg.a);
>>     writefln(arg.b);
>>     writefln(arg.c);
>> }
>>
>> int main(){
>>     func(15, 30, 45); // Full initalization
>>     func(b : 45, c : 30); // Partial initalization
>> }
>>
> Unless I misunderstand you, it seems you are suggesting named 
> parameters. Some other languages have this already (for example, OCaml), 
> but it doesn't require structs to do this. You can pick up the names of 
> parameters from the prototype, and we already have support for default 
> parameters. All you need now is support for naming parameters at the 
> call-site, for which your syntax seems perfectly suitable. Then, you 
> could avoid variadic functions and structs and just write:
> 
>   FilePtr openFile(char[] filename, bool read=true, bool write=true, 
> bool append=false, bool anotherParam=false) {...}
> 
> int main(){
>     openFile(filename : "asdf.txt", append : true);
>     // Equivalent to openFile("asdf.txt", true, true, true, false);
> }
> 
> Which is certainly a nice syntax feature to have.

This has been suggested several times.

It may be possible.  But I recall Walter was uncomfortable with 
parameter names in prototypes becoming significant all the sudden when 
they aren't in C/C++.  Prototypes currently don't require a parameter to 
have a name at all.   Then there's the added difficulty of figuring out 
overloading.    If you have two functions with a parameter called 'foo' 
which one do you call for func(foo:23).  Some new rules for what's the 
best match are probably needed.  I don't think the issues are 
insurmountable, but they do require some work.  And a willingness on 
everyone's part to accept that the _name_ of a parameter is now part of 
the public interface and thus changing a parameter name can break code. 
   I don't recall about OCaml, but Python at least has keyword arguments 
but doesn't have overloading, so it doesn't have to deal with the 
overload/keyword interaction.

Also I'd say that if regular functions get call-by keyword capability, 
then I would hope that templates get it too.  So that may have some 
implications on the syntax used.  If "keyword : value" is used for 
functions, then probably something different would have to be used for 
templates, since : already means specialization there.  It would be nice 
if the same syntax could be used for both.

Here are some links --

Thread started by Chris Sauls Jun 2005:
http://www.digitalmars.com/d/archives/digitalmars/D/25835.html

Thread started by me May 2005:
http://www.digitalmars.com/d/archives/digitalmars/D/23172.html

Thread started by Norbert Nemec, June 2004
http://www.digitalmars.com/d/archives/digitalmars/D/4543.html

Thread started by Eric Shumard, May 2004
http://www.digitalmars.com/d/archives/digitalmars/D/752.html

And this one's not about keyword args, but about default parameters and 
function pointers in general. Started by Don Clugston.
http://www.digitalmars.com/d/archives/digitalmars/D/27291.html


--bb



More information about the Digitalmars-d mailing list