Thread overview
Traits and functions
Jan 10, 2015
Bauss
Jan 10, 2015
Ali Çehreli
Jan 11, 2015
Bauss
January 10, 2015
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.
January 10, 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

January 11, 2015
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!