Help with reflection?
bitwise via Digitalmars-d
digitalmars-d at puremagic.com
Fri Oct 10 16:44:36 PDT 2014
On Friday, 10 October 2014 at 17:43:13 UTC, Kapps wrote:
> On Friday, 10 October 2014 at 17:00:01 UTC, bitwise wrote:
>> Hey, I am trying to put together a basic reflection system,
>> but I've run into a problem. I'm not sure if this is a bug or
>> not.
>>
>> I am trying to store method information about a class. So
>> basically, I am using Traits to go through all members of the
>> class, and then trying to create delegates to each member.
>> Then, at runtime, the function will be dynamically invoked by
>> changing the ".ptr" property of the delegate, and calling it.
>>
>> These are the errors I am getting from these 3 lines from the
>> code below:
>>
>> //del.ptr = null;
>> -Error: CTFE internal error: unsupported assignment del.ptr =
>> null (TestD)
>>
>> //del.funcptr = mixin("&T." ~ member);
>> -Error: CTFE internal error: unsupported assignment
>> del.funcptr = & someFunction (TestD)
>> -Also, why has 'T' been removed after mixin?
>>
>> //del.funcptr = mixin("&SomeClass." ~ member);
>> -Error: CTFE internal error: unsupported assignment
>> del.funcptr = & someFunction (TestD)
>
> Without looking too much at your code, the internal error seems
> to definitely imply a bug, I recommend reporting it on the
> issue tracker.
>
> Also, I made a Reflection module as well, you may find some of
> the code useful (such as getting methods at compile-time and
> invoking at run-time):
> https://shardsoft.com/stash/projects/SHARD/repos/shardtools/browse/source/ShardTools/Reflection.d?until=45ded3019f3f05d7b68e5746d34da1de7433ccf6#1299
> (links to a specific commit, after that I started trying to
> unify runtime and compile-time reflection into one API which
> gave me some troubles). It can get a bit complicated depending
> on what your needs are, as you would need to deal with vtables
> and interfaces depending on what you want to support. My
> implementation definitely isn't perfect, but works for my usage.
Thanks for the help!
I tried a workaround, which was to store the function pointer
and then assign it to the delegate at runtime, at which point the
compiler told me that the function pointer was not a compile time
constant. So that may have been the root of the problem.
Finally, I was able to get my code working by accessing the
vtable directly like your code.
My code is posted here:
https://github.com/bitwise-github/D-Reflection
More information about the Digitalmars-d
mailing list