www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Traits and functions

reply "Bauss" <jj_1337 live.dk> writes:
Is there a way to get all functions within a module using traits? 
I tried "allMembers" and it seem to work, but I can't use 
"getFunctionAttributes" with it and if I use "getAttributes" then 
it won't find any applied attributes.

What I do is having a package module with a staic constructor 
which loops through "allMembers" and then I want to find 
functions with a specific attribute. All the members are imported 
using public imports. However it can find the specific functions, 
but it does not find the attributes.
Jan 10 2015
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 01/10/2015 08:21 AM, Bauss wrote:
 Is there a way to get all functions within a module using traits? I
 tried "allMembers" and it seem to work, but I can't use
 "getFunctionAttributes" with it and if I use "getAttributes" then it
 won't find any applied attributes.

 What I do is having a package module with a staic constructor which
 loops through "allMembers" and then I want to find functions with a
 specific attribute. All the members are imported using public imports.
 However it can find the specific functions, but it does not find the
 attributes.
The following program prints both the function attributes and user defined attributes e.g. of foo(): module deneme; import std.string; import std.traits; struct MyAttr {} MyAttr void foo(int i, double d) pure nogc nothrow property {} void main() { foreach (m; __traits(allMembers, deneme)) { pragma(msg, format("module member: %s", m)); static if (mixin ("isCallable!" ~ m)) { pragma(msg, format("%s is callable", m)); foreach (funcAttr; mixin (format("__traits(getFunctionAttributes, %s)", m))) { pragma(msg, format(" function attribute: %s", funcAttr)); } foreach (attr; mixin (format("__traits(getAttributes, %s)", m))) { static if (is (attr == MyAttr)) { pragma(msg, format(" uda: %s", attr.stringof)); } } } } } Ali
Jan 10 2015
parent "Bauss" <jj_1337 live.dk> writes:
On Saturday, 10 January 2015 at 23:23:52 UTC, Ali Çehreli wrote:
 On 01/10/2015 08:21 AM, Bauss wrote:
 Is there a way to get all functions within a module using 
 traits? I
 tried "allMembers" and it seem to work, but I can't use
 "getFunctionAttributes" with it and if I use "getAttributes" 
 then it
 won't find any applied attributes.

 What I do is having a package module with a staic constructor 
 which
 loops through "allMembers" and then I want to find functions 
 with a
 specific attribute. All the members are imported using public 
 imports.
 However it can find the specific functions, but it does not 
 find the
 attributes.
The following program prints both the function attributes and user defined attributes e.g. of foo(): module deneme; import std.string; import std.traits; struct MyAttr {} MyAttr void foo(int i, double d) pure nogc nothrow property {} void main() { foreach (m; __traits(allMembers, deneme)) { pragma(msg, format("module member: %s", m)); static if (mixin ("isCallable!" ~ m)) { pragma(msg, format("%s is callable", m)); foreach (funcAttr; mixin (format("__traits(getFunctionAttributes, %s)", m))) { pragma(msg, format(" function attribute: %s", funcAttr)); } foreach (attr; mixin (format("__traits(getAttributes, %s)", m))) { static if (is (attr == MyAttr)) { pragma(msg, format(" uda: %s", attr.stringof)); } } } } } Ali
Thank you this was exactly what I was looking for!
Jan 10 2015