www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - how to properly compare this type?

reply Jack <jckj33 gmail.com> writes:
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
next sibling parent frame <frame86 live.com> writes:
On Tuesday, 9 February 2021 at 23:12:57 UTC, Jack wrote:

 arr is empty
You need to check for __traits(getOverloads,...), isCallable!, ReturnType!.
Feb 09
prev sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
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
parent Jack <jckj33 gmail.com> writes:
helpful always,thank you guys
Feb 12