Make `& Class.foo` illegal

Jacob Carlborg doob at me.com
Fri Aug 28 12:51:23 UTC 2020


On Thursday, 27 August 2020 at 11:20:17 UTC, Johan wrote:
> Currently this code is not rejected by the compiler and instead 
> creates non-functioning (undefined) code:
> ```
> struct S {
>     void foo() {}
> }
>
> void main() {
>     auto a = &S.foo;
> }
> ```
>
> See https://issues.dlang.org/show_bug.cgi?id=21195 .
>
> Considering https://www.digitalmars.com/articles/b68.html, why 
> not simply reject `&S.foo` during semantic analysis?
>
> -Johan

As others have mentioned, it's possible to manually assemble a 
delegate from a context pointer and a function pointer. I've used 
this technique in my Objective-C bridge. In that case the context 
pointer (or this pointer for a class) came from Objective-C and 
the function pointer from D. Here's a link to the code for 
reference [1].

[1] 
http://dsource.org/projects/dstep/browser/dstep/objc/bridge/Bridge.d#L377

--
/Jacob Carlborg


More information about the Digitalmars-d mailing list