Function pointers/delegates default args were stealth removed?

Chris Cain clcain at uncg.edu
Sun Aug 26 20:49:03 PDT 2012


If I may, here's my suggestion on how everything could work (and 
I think should be doable) ... basically, it would be part of the 
type, but functions with the same parameters regardless of 
defaults would be implicitly convertible to one another:

------

void efpBad( int function(int) fun ) {
     return fun(); // Fails to compile, as expected
}

void efp1( int function(int) fun ) {
     return fun(1); // Compiles fine, works as expected
}

void efp1Def1( int function(int = 1) fun ) {
     return fun(); // Compiles fine, equivalent to fun(1) which 
should be expected
}


auto fn1p = (int x) { return x; };
auto fn1pDef = (int x = 2) { return x; };

auto fn2p = (int x, int y) { return x + y; };
auto fn2pDef = (int x, int y = 2) { return x + y; };


efp2(fn2p); // Fails to compile, obviously ... # of parameters 
don't match.
efp2(fn2pDef); // Fails to compile, # of parameters don't match 
(even though 1 has provided a "default")
efp2(fn1p); // Success ... returns 1 as expected
efp2(fn1pDef); // Ditto
efp3(fn1p); // Compiles ... implicitly convertible, returns 1 
(efp3 is providing the default above)
efp3(fn1pDef); // Successfully compiles, returns 1 ... NOT 5 
(because 1 is provided as the default in efp3 above)

// Implicitly convertible between variables
fn1p = fn1pDef; // Perfectly fine
fn1p(); // Fails to compile, must provide an argument!
fn1pDef = fn1p; // Again, fine. fn1pDef will allow the arg to 
have the default of 1 now.
fn1pDef(); // Succeeds, returns 1

fn1p = fn2pDef; // Fails to compile, as expected
fn1p = fn2p; // Nonsense, so it fails, of course

// IsExpressions
is(typeof(fn1p) == typeof(fn1pDef)); // False
is(typeof(fn1p) : typeof(fn1pDef));  // True
is(typeof(fn1pDef) : typeof(fn1p));  // True

I hope that was rigorous enough to show the behavior when passing 
around and such. I feel like this behavior would be acceptable 
for almost every use. I'm pretty sure this is how people were 
expecting it to behave before, anyway.

Is there a problem with it working this way? (Obviously, I'm no 
compiler expert)


More information about the Digitalmars-d mailing list