Structs and Variadic Functions (Keyword arguments)

Xinok xnknet at gmail.com
Tue Jan 9 09:52:35 PST 2007


Xinok Wrote:

> 
> While this post is still here, I'd like to make this suggestion:
> We have the 'default' keyword, let's use it!
> 
> void func(int a = 15, int b = 30, int c = 45);
> func(45, default, 60);

Just one more thing I'd like to add, this could propose a slight problem with overloading functions:

void func(int a = 15, int b = 30, int c = 45);
void func(int a = 15, string b = "Thirty", int c = 45);
func(45, default, 60); // Error - Cannot overload default argument

A simple suggestion for this:
func(45, default int, 60);
func(45, default string, 60);



To the original post, perhaps we could introduce a new kind of default parameter to D? Something like a 'static default argument'.

> PROPOSAL
> The solution is really very simple.
> 
> int afunc(int x, int y=3)
> {
> ..dosomething
> }
> 
> should be *identical* to declaring TWO functions:
> 
> int afunc(int x)
> {
> afunc(x, 3);
> }
> 
> int afunc(int x, int y)
> {
> .. dosomething
> }

I know nothing about assembly, but would it be possible to simply put all the initalizers into a single function, and depending on the type of the function pointer, it jumps to the proper location in the function to initalize the rest of the parameters?

int func(int a = 15, int b = 30, int c = 45){
	a = 15; // int func()
	b = 30; // int func(int)
	c = 45; // int func(int, int)
	return a + b + c; // int func(int, int, int)
}

IAW, if you were to create a pointer to this function:
int delegate(int, int) ptr = &func;
This delegate would point directly to the address of the code where you see 'c = 45'

This would eliminate any performance issues and 'ghost functions'.



More information about the Digitalmars-d mailing list