digitalmars.D.learn - Iterate through getOverloads trait
- Max Klimov (6/6) Jul 15 2016 I'm wondering how I can use the output of __traits(getOverloads,
- =?UTF-8?Q?Ali_=c3=87ehreli?= (48/56) Jul 16 2016 That works but what is produced is just something that represents the
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
Jul 15 2016
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 resultThat 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/28a0057dbffdIn 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
Jul 16 2016