Structs and Variadic Functions

Reiner Pope xxxxxx at xxx.xx
Wed Dec 20 21:46:14 PST 2006


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.

Cheers,

Reiner



More information about the Digitalmars-d mailing list