Traits and functions
Ali Çehreli via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Jan 10 15:23:52 PST 2015
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
More information about the Digitalmars-d-learn
mailing list