Thread overview
how to properly compare this type?
Feb 09, 2021
Jack
Feb 10, 2021
frame
Feb 13, 2021
Jack
February 09, 2021
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
February 10, 2021
On Tuesday, 9 February 2021 at 23:12:57 UTC, Jack wrote:

> arr is empty

You need to check for __traits(getOverloads,...), isCallable!, ReturnType!.

February 11, 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
February 13, 2021
helpful always,thank you guys