Thread overview
Determine if a member is a method
Jan 01, 2014
Frustrated
Jan 01, 2014
Orvid King
Jan 01, 2014
Jacob Carlborg
Jan 01, 2014
Jacob Carlborg
Jan 01, 2014
Gary Willoughby
Jan 01, 2014
Dicebot
Jan 01, 2014
Frustrated
Jan 01, 2014
Dicebot
January 01, 2014
How does one determine if a member is a method and not anything else?

Also, how does one get the exact code string of a member instead of having to piece it together from info from std.traits? (which requires a lot of work)?

January 01, 2014
Well, unashamedly copying from my own code, I have a template defined thusly:

enum isMemberFunction(T, string member) =
is(typeof(__traits(getMember, T.init, member)) == function);

Where `T` is the type that `member` is a part of. You can also change `function` to any of class, interface, struct, enum, or union, do find out if the member is one of those. The only way I know of to determine if a member is a field though, is to determine that it is not a class, function, interface, struct, enum, or union.

As to the second question, I suspect I'm failing at understanding what your asking, so I'll leave it to someone else (who's probably a bit more awake than I am) to answer that one.

On 1/1/14, Frustrated <c1514843@drdrb.com> wrote:
> How does one determine if a member is a method and not anything else?
>
> Also, how does one get the exact code string of a member instead of having to piece it together from info from std.traits? (which requires a lot of work)?
>
>
January 01, 2014
On 2014-01-01 08:55, Orvid King wrote:
> Well, unashamedly copying from my own code, I have a template defined thusly:
>
> enum isMemberFunction(T, string member) =
> is(typeof(__traits(getMember, T.init, member)) == function);
>
> Where `T` is the type that `member` is a part of. You can also change
> `function` to any of class, interface, struct, enum, or union, do find
> out if the member is one of those. The only way I know of to determine
> if a member is a field though, is to determine that it is not a class,
> function, interface, struct, enum, or union.

To find out if a member is a field you can iterate T.tupleof:

https://github.com/jacob-carlborg/mambo/blob/master/mambo/util/Reflection.d#L195

-- 
/Jacob Carlborg
January 01, 2014
On 2014-01-01 08:43, Frustrated wrote:

> Also, how does one get the exact code string of a member instead of
> having to piece it together from info from std.traits? (which requires a
> lot of work)?

You mean the to get the full signature of a method? I don't think that's possible, unless you can use __PRETTY_FUNCTION__ [1] somehow.

[1] http://dlang.org/traits.html#specialkeywords

-- 
/Jacob Carlborg
January 01, 2014
On Wednesday, 1 January 2014 at 12:19:29 UTC, Jacob Carlborg
wrote:
> On 2014-01-01 08:43, Frustrated wrote:
>
>> Also, how does one get the exact code string of a member instead of
>> having to piece it together from info from std.traits? (which requires a
>> lot of work)?
>
> You mean the to get the full signature of a method? I don't think that's possible, unless you can use __PRETTY_FUNCTION__ [1] somehow.
>
> [1] http://dlang.org/traits.html#specialkeywords

I have some useful templates in the source of DUnit which does
this. Take a look and customise for what you need:

https://github.com/nomad-software/dunit/blob/master/source/dunit/reflection.d#L639
January 01, 2014
> On 2014-01-01 08:43, Frustrated wrote:
>
>> Also, how does one get the exact code string of a member instead of
>> having to piece it together from info from std.traits? (which requires a
>> lot of work)?

Have a look at https://github.com/rejectedsoftware/vibe.d/blob/master/source/vibe/internal/meta/codegen.d#L177 (cloneFunction) for inspiration (it is based on fullyQualifiedName from Phobos)
January 01, 2014
On Wednesday, 1 January 2014 at 15:10:56 UTC, Dicebot wrote:
>> On 2014-01-01 08:43, Frustrated wrote:
>>
>>> Also, how does one get the exact code string of a member instead of
>>> having to piece it together from info from std.traits? (which requires a
>>> lot of work)?
>
> Have a look at https://github.com/rejectedsoftware/vibe.d/blob/master/source/vibe/internal/meta/codegen.d#L177 (cloneFunction) for inspiration (it is based on fullyQualifiedName from Phobos)

There seems to be a bug. When I run it on a standard member it works fine. When I run it on a property it throws an error

src\phobos\std\traits.d(344): Error: forward reference of variable parentPrefix
src\phobos\std\traits.d(505): Error: template instance std.traits.fullyQualifiedNameImplForSymbols!(Array!double) error instantiating
src\phobos\std\traits.d(295):        instantiated from here: fullyQualifiedNameImplForTypes!(Array!double, false, false, false, false)

(admittedly it might not suppose be used on properties but it shouldn't crash and burn. Not sure if this is an issue with vibe.d or phobos)
January 01, 2014
On Wednesday, 1 January 2014 at 17:19:01 UTC, Frustrated wrote:
> There seems to be a bug. When I run it on a standard member it works fine. When I run it on a property it throws an error
>
> src\phobos\std\traits.d(344): Error: forward reference of variable parentPrefix
> src\phobos\std\traits.d(505): Error: template instance std.traits.fullyQualifiedNameImplForSymbols!(Array!double) error instantiating
> src\phobos\std\traits.d(295):        instantiated from here: fullyQualifiedNameImplForTypes!(Array!double, false, false, false, false)
>
> (admittedly it might not suppose be used on properties but it shouldn't crash and burn. Not sure if this is an issue with vibe.d or phobos)

Looks like it is an issue with return type which is template struct/class. There is a long-standing bug with fullyQualifiedName in that regard: https://d.puremagic.com/issues/show_bug.cgi?id=10502

I am aware of that issue but don't have a good generic solution for it within existing D reflection tools.

Properties on their own should work fine.