www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Reading Atributes (UDA) of overloaded functions

reply Ozan <ozan.nurettin.sueel sap.com> writes:
Hi!

Is it possible to read all attributes in case of overloading 
functions? Example:

struct Att { string name; }
struct Att2 { string name; }
 Att void testUDA(string x) { writeln("test(string ",x,")"); }
 Att2 void testUDA(int x) { writeln("test(string ",x,")"); }

void main(string[] args)
{
	testUDA("aaa");
	testUDA(100);

	foreach (attr; __traits(getAttributes, testUDA)) {
		if (attr.stringof == "Att") writeln("Found Att");
		if (attr.stringof == "Att2") writeln("Found Att2");
	}
}

results in one (!) "Found Att".
Seems like "First comes, first wins".

Regards, Ozan
Sep 17 2015
parent reply John Colvin <john.loughran.colvin gmail.com> writes:
On Thursday, 17 September 2015 at 11:47:40 UTC, Ozan wrote:
 Hi!

 Is it possible to read all attributes in case of overloading 
 functions? Example:

 struct Att { string name; }
 struct Att2 { string name; }
  Att void testUDA(string x) { writeln("test(string ",x,")"); }
  Att2 void testUDA(int x) { writeln("test(string ",x,")"); }

 void main(string[] args)
 {
 	testUDA("aaa");
 	testUDA(100);

 	foreach (attr; __traits(getAttributes, testUDA)) {
 		if (attr.stringof == "Att") writeln("Found Att");
 		if (attr.stringof == "Att2") writeln("Found Att2");
 	}
 }

 results in one (!) "Found Att".
 Seems like "First comes, first wins".

 Regards, Ozan
use __traits(getAttributes, /*...*/) on each of the members of the result of __traits(getOverloads, /*...*/)
Sep 17 2015
parent reply Ozan <ozan.nurettin.sueel sap.com> writes:
On Thursday, 17 September 2015 at 12:36:42 UTC, John Colvin wrote:
 On Thursday, 17 September 2015 at 11:47:40 UTC, Ozan wrote:
...
 use __traits(getAttributes, /*...*/) on each of the members of 
 the result of __traits(getOverloads, /*...*/)
Great! Thanks! Now it works: foreach (ov; __traits(getOverloads, Test, "testUDA")) { foreach (attr; __traits(getAttributes, ov)) { if (attr.stringof == "Att") writeln("Found Att"); if (attr.stringof == "Att2") writeln("Found Att2"); if (attr.stringof == "Att3") writeln("Found Att3"); } } Regards, Ozan
Sep 17 2015
parent John Colvin <john.loughran.colvin gmail.com> writes:
On Thursday, 17 September 2015 at 12:40:24 UTC, Ozan wrote:
 On Thursday, 17 September 2015 at 12:36:42 UTC, John Colvin 
 wrote:
 On Thursday, 17 September 2015 at 11:47:40 UTC, Ozan wrote:
...
 use __traits(getAttributes, /*...*/) on each of the members of 
 the result of __traits(getOverloads, /*...*/)
Great! Thanks! Now it works: foreach (ov; __traits(getOverloads, Test, "testUDA")) { foreach (attr; __traits(getAttributes, ov)) { if (attr.stringof == "Att") writeln("Found Att"); if (attr.stringof == "Att2") writeln("Found Att2"); if (attr.stringof == "Att3") writeln("Found Att3"); } } Regards, Ozan
Awesome. By the way, in the next release there will be some more tools in phobos for this stuff, see http://dlang.org/phobos-prerelease/std_traits.html#getSymbolsByUDA for a taster.
Sep 17 2015