Generate a pointer to a method of a struct

Jack Pope zedlan at invec.net
Sat Oct 15 15:14:57 UTC 2022


On Saturday, 15 October 2022 at 01:48:15 UTC, kdevel wrote:
> $ dmd -unittest -main -run ini
> ini.d(6): Error: non-constant expression `& bar`
> ini.d(7): Error: non-constant expression `& bar`
> ```
>
> Is this consistent?

I can attest to consistency using ldc. Each of the following 
prevent function address re-assignment:

   immutable string function() f = &S.bar;
   const string function() f = &S.bar;
   enum f = &S.bar;

In contrast, as would be expected, the following has no such 
restriction:

   string function() f = &S.bar;

Example:

   struct S {
     string bar(){return "Hello";}
     string foo(){return "Goodbye";}
   }

   void main() {
     string function() f = &S.bar;
     writeln(f, " ", f());
     f = &S.foo;
     writeln(f, " ", f());
   }




More information about the Digitalmars-d-learn mailing list