July 15, 2016 Iterate through getOverloads trait | ||||
---|---|---|---|---|
| ||||
I'm wondering how I can use the output of __traits(getOverloads, a, "name"). The example in the doc uses direct indexing (like __traits(getOverloads, a, "name")[0](param)) and it works. But I'm struggling with iterating through the result or storing the resulting tuple into a local var. Example: https://dpaste.dzfl.pl/28a0057dbffd |
July 16, 2016 Re: Iterate through getOverloads trait | ||||
---|---|---|---|---|
| ||||
Posted in reply to Max Klimov | On 07/15/2016 03:58 PM, Max Klimov wrote: > I'm wondering how I can use the output of __traits(getOverloads, a, > "name"). Apparently, it produces a delegate of calling name() on 'a'. > The example in the doc uses direct indexing (like > __traits(getOverloads, a, "name")[0](param)) and it works. But I'm > struggling with iterating through the result That works but what is produced is just something that represents the member function. You can use it to further iterate over its e.g. attributes. Here is an excerpt that I've found. Note that 'tp' is a *type* in the following code: foreach(m; __traits(allMembers, tp)) { foreach(o; __traits(getOverloads, tp, m)) { foreach(attr; __traits(getAttributes, o)) { static if (is(attr == get)) { // ... } else static if (is(attr == set)) { // ... } } } } > or storing the resulting > tuple into a local var. > Example: https://dpaste.dzfl.pl/28a0057dbffd In that case, you use an object ('a') instead of its type: import std.stdio; class A { void foo() { writeln("A::foo"); } int foo() const { writeln("A.foo const"); return 42; } } int main() { auto a = new A; auto d = &__traits(getOverloads, a, "foo")[0]; pragma(msg, typeof(d)); d(); return 0; } So, the & character makes a delegate of calling foo() on 'a' and we're able to call it. However, I failed to make this work with a compile-time foreach. @OTHERS: How would you iterate over both overloads of foo() in the above code? (Probably we can use string mixins starting with the .length of __traits(getOverloads).) Please create as many bug reports as necessary along the way. ;) Ali |
Copyright © 1999-2021 by the D Language Foundation