Method pointers are *function* pointers?? Or delegates??

Jacob Carlborg doob at me.com
Tue May 22 11:28:33 PDT 2012


On 2012-05-22 20:14, Steven Schveighoffer wrote:

> I agree, it's unsound. But so is this:
>
> int *blah = void;
>
> *blah = 5;
>
> It doesn't mean that the language should forbid it, or that the compiler
> isn't implemented as designed.
>
> At the *very least*, the address to member function operation should be
> illegal in @safe code.
>
>> At best things could be arranged that &Test.foo has type void
>> function(Test) or something.
>
> I would suggest that it should be:
>
> function(Test this) with the 'this' being mangled into the name, and
> affect the calling convention.
>
> Structs would be function(ref Test this).
>
> And const/shared/immutable decorations should apply properly to the
> 'this' parameter.
>
> I'd wholeheartedly support such an improvement. In fact, I'd be willing
> to write a DIP on it, if Walter had a chance of approving it. I just
> don't know if it would happen...
>
> -Steve

It needs to be possible to compose delegates:

class Foo
{
     void foo () {};
}

void delegate () dg;

dg.funcptr = &Foo.foo;
dg.ptr = cast(void*) new Foo;
dg();

At least it needs to be possible to do that in code marked with @system.

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list