Function pointers/delegates default args were stealth removed?

Steven Schveighoffer schveiguy at yahoo.com
Mon Aug 27 07:16:57 PDT 2012


On Sun, 26 Aug 2012 18:26:40 -0400, Manu <turkeyman at gmail.com> wrote:

> I just updated to 2.60 and found errors throughout my code where function
> pointers default args no longer work.
> *Every single project* I've written in D, 5 projects, don't work anymore,
> including my projects at work.

OK, I've read all the posts on this, and this is what I think would solve  
the problem:

1. default parameters are part of the function pointer type, because a  
function pointer has a type.
2. The mangled name of the function that is assigned to that function  
pointer does *not* have default parameters mangled in.  Only the function  
pointer type has them.
3. Since the default parameters are part of the type, but not defined by  
the function it points to, you can use interchangeably functions of the  
same type which define default parameters or not (or define different  
ones).  The default parameters follow the function pointer variable.
4. If a template instantiates with a function type that takes a default  
parameter, that default parameter is mangled as part of the template  
type.  There is no way around this.  But the mangling of the default  
parameter is *separate* from the mangling of the function type.

We can probably implement this as a library feature, but I lack the full  
template-fu skills to do it.  It would be nice to have the compiler do  
this for us, but I don't know the work involved.  Here is a pseudocode  
type we may be able to use:

struct DefaultParamFuncPtr(F, alias T...) if(is(F == function) &&  
isValues!T) // no idea how to define isValues
{
    F funcptr;
    static T defaultparams;
    alias funcptr this; // allow this type to act as a straight F, or  
accept an F to rebind to a different function
    typeof(funcptr()) opCall(X...)(X params) if(X is a prefix of F's args)  
// no idea how to write this constraint :)
    {
       // construct the rest of the tuple for params given defaultparams
       typeof(F's args) realtuple = params ~  
defaultparams[params.length..$]; // construct the full call parameters
       return funcptr(realtuple);
    }
}

Yeah, it would be really nice to do this in the compiler, but I think the  
above describes what I would consider to be an equivalent type.  The  
default parameters live as metadata in the type of the function pointer,  
but are separate from the function type that it points to.

-Steve


More information about the Digitalmars-d mailing list