digitalmars.D.learn - how to properly compare this type?
- Jack (33/33) Feb 09 2021 I have a class like this:
- frame (3/4) Feb 09 2021 You need to check for __traits(getOverloads,...), isCallable!,
- Steven Schveighoffer (11/13) Feb 11 2021 That's not what you want. string function(string) is a *pointer* to a
- Jack (1/1) Feb 12 2021 helpful always,thank you guys
I have a class like this: struct S { } class A { (S) { int a; string b() { return ib; } string b(string s) { return ib = s;} } int x; int y; string ib = "lol"; } where I want to list the members that have S UDA but it failing to compare string b() { ... }. How do I do this? current code: import std.traits : hasUDA; string[] arr; static foreach(member; __traits(allMembers, A)) { static if(hasUDA!(__traits(getMember, A, member), S)) { pragma(msg, typeof(__traits(getMember, A, member))); static if(is(typeof(__traits(getMember, A, member)) == string function(string))) { arr ~= member; } } } writeln(arr); arr is empty
Feb 09 2021
On Tuesday, 9 February 2021 at 23:12:57 UTC, Jack wrote:arr is emptyYou need to check for __traits(getOverloads,...), isCallable!, ReturnType!.
Feb 09 2021
On 2/9/21 6:12 PM, Jack wrote:static if(is(typeof(__traits(getMember, A, member)) == string function(string)))That's not what you want. string function(string) is a *pointer* to a function that accepts a string and returns a string. In addition to getting the overloads (you only get one "b" in the list of members), take the address of the overload. This worked for me: foreach(overload; __traits(getOverloads, A, member)) static if(is(typeof(&overload) == string function(string))) { arr ~= member; } -Steve
Feb 11 2021