Thread overview | |||||
---|---|---|---|---|---|
|
January 10, 2015 Traits and functions | ||||
---|---|---|---|---|
| ||||
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 Re: Traits and functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bauss | 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 Re: Traits and functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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!
|
Copyright © 1999-2021 by the D Language Foundation