www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Pointers to member functions part 2

D does not have member function pointers, but currently the following will 
have a and b be of type 'void function()'.

class C { void func(); }

void main()
{
    auto c = new C;

    auto a = &C.func;
    auto b = (&c.func).funcptr;
}

I'd like to change &ClassType.nonstaticmemberfunction and delegate.funcptr 
to both return void* and make delegate.ptr and delegate.funcptr both 
read-only.  This would disallow the following nonsense code.

class Class
{
    void func() {}
}
class OtherClass
{
    void func() {}
}

void main()
{
    auto instance = new Class();
    void delegate x = &instance.func;
    x.ptr = new int;
    x.funcptr = &OtherClass.func;
    x();
    auto funptr  = &Class.func;
    funptr();
}

You can still of course modify delegates using casts or unions if you have 
to, but as it's something unsafe you shouldn't be able to do it without 
them.

Can anybody see a reason these changes wouldn't be an improvement to the 
language?
If not I'll put in a pull request for it. 
Jun 18 2011