[Issue 21862] Taking address of non-static method without "this" should not be allowed

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Dec 6 07:53:17 UTC 2022


https://issues.dlang.org/show_bug.cgi?id=21862

--- Comment #3 from RazvanN <razvan.nitu1305 at gmail.com> ---
(In reply to Eyal from comment #2)
> Why would you take the function ptr of a delegate without the ctx, except as
> a "void*" value to compare against?
> 
> A delegate's function ptr is *not* callable without the context, so it
> should not have the type of callable things.
> 
> If the test suite assumes the delegate is implicitly convertible to a
> callable function without the context -- the test suite should be fixed, as
> this is non-sensical.

You are correct that the function ptr of a delegate should not be callable
without it's context however, you can manually set the context ptr and the
function ptr of delegates.

```d
    struct S {
        int m(int x) {return x;}
    }

    void main() {
        import std.stdio;
        int function(int) func = &S.m;
        int delegate(int) func2;
        S s;
        func2.funcptr = func;
        func2.ptr = &s;
        writeln(func2(5));
    }
```

&S.m returns a function pointer so technically func is callable. It is up to
the user to make sure that he is using them properly. So right now we cannot
disallow taking the adddress of S.m. The only way this could be fixed would be
to change the type of &S.m something else, but I think that this will cause
more problems.

--


More information about the Digitalmars-d-bugs mailing list