IFTI reflection would be nice

user1234 user1234 at 12.de
Tue Jul 28 07:46:36 UTC 2020


On Sunday, 26 July 2020 at 14:03:31 UTC, Avrina wrote:
> On Sunday, 26 July 2020 at 13:20:40 UTC, Adam D. Ruppe wrote:
>> Imagine:
>>
>> void foo(int a) {}
>> void foo(string b) {}
>>
>>
>> I want to get the address of foo(string). How do you do that 
>> right now? Well, you have to loop over overloads and check the 
>> types. What a pain, but at least doable.
>>
>> But wouldn't it be cool if you could do
>>
>> &__functionOf(foo("foo"))
>>
>> Where the __functionOf is the magic.
>
> You can just use cast() right now to get the overload you want.
>
> https://run.dlang.io/is/n76kt3
>
> import std.stdio;
>
> void foo(string) { writeln("foo(string)"); }
> void foo(int)    { writeln("foo(int)"); }
>
> void main() {
>     auto a = cast(void function(string))&foo;
>     auto b = cast(void function(int))&foo;
>     a("");
>     b(10);
> }

OMG  excellent, where have you learnt this ancient secret of the 
gods ?


More information about the Digitalmars-d mailing list