www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Determine if a member is a method

reply "Frustrated" <c1514843 drdrb.com> writes:
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)?
Dec 31 2013
next sibling parent reply Orvid King <blah38621 gmail.com> writes:
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)?
Dec 31 2013
parent Jacob Carlborg <doob me.com> writes:
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
Jan 01 2014
prev sibling parent reply Jacob Carlborg <doob me.com> writes:
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
Jan 01 2014
next sibling parent "Gary Willoughby" <dev nomad.so> writes:
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
Jan 01 2014
prev sibling parent reply "Dicebot" <public dicebot.lv> writes:
 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)
Jan 01 2014
parent reply "Frustrated" <c1514843 drdrb.com> writes:
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)
Jan 01 2014
parent "Dicebot" <public dicebot.lv> writes:
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.
Jan 01 2014