Thread overview |
---|
January 23, 2018 getting member functions of a struct and Error: identifier expected following ., not this | ||||
---|---|---|---|---|
| ||||
Hi, I'm trying to get a list of only member functions of a struct. I've found that if you do not declare a struct as static inside a scope, then there's a hidden "this" member as part of the struct. Can someone explain the logic there? Also am I going about this correctly? template MemberFunctions(T) { import std.traits: isFunction; auto MemberFunctions() { string[] memberFunctions; foreach (member; __traits(allMembers, T)) { // NOTE: This static if is here is because of that hidden "this" member // // if I do not do this then I get: // Error: identifier expected following ., not this static if (is(typeof(mixin("T." ~ member)) F)) if (isFunction!F) { memberFunctions ~= member; } } return memberFunctions; } } unittest { // works for static and non static. /* static */ struct A { void opCall() {} void g() {} } /* static */ struct B { int m; A a; alias a this; void f() {} } static assert(MemberFunctions!B == ["f"]); } Cheers |
January 23, 2018 Re: getting member functions of a struct and Error: identifier expected following ., not this | ||||
---|---|---|---|---|
| ||||
Posted in reply to aliak | On Tuesday, 23 January 2018 at 00:00:38 UTC, aliak wrote:
> Hi, I'm trying to get a list of only member functions of a [...]
> Cheers
And a follow up question: should I be using static foreach in there instead of a normal foreach? i.e.
foreach (member; __traits(allMembers, T)) {{
// same code
}}
And why if yes
Thanks again!
|
January 24, 2018 Re: getting member functions of a struct and Error: identifier expected following ., not this | ||||
---|---|---|---|---|
| ||||
Posted in reply to aliak | On Tuesday, 23 January 2018 at 00:00:38 UTC, aliak wrote:
> Hi, I'm trying to get a list of only member functions of a struct. I've found that if you do not declare a struct as static inside a scope, then there's a hidden "this" member as part of the struct. Can someone explain the logic there?
The struct defined inside a scope can mention variables defined in that scope (e.g. use them in its methods), so it needs a pointer to the place where those closed variables live. That's the main difference between such struct and a static one that cannot use those scope vars. I guess you're seeing that pointer as additional member.
As for static foreach, when you write a simple foreach over some compile-time tuple (like in this case), it's unrolled at compile time similarly to "static foreach", the main difference is whether it creates a sub-scope for the loop body or not. "foreach" creates one, "static foreach" doesn't.
|
January 24, 2018 Re: getting member functions of a struct and Error: identifier expected following ., not this | ||||
---|---|---|---|---|
| ||||
Posted in reply to thedeemon | On Wednesday, 24 January 2018 at 07:55:01 UTC, thedeemon wrote:
> On Tuesday, 23 January 2018 at 00:00:38 UTC, aliak wrote:
>> [...]
>
> The struct defined inside a scope can mention variables defined in that scope (e.g. use them in its methods), so it needs a pointer to the place where those closed variables live. That's the main difference between such struct and a static one that cannot use those scope vars. I guess you're seeing that pointer as additional member.
>
>
> As for static foreach, when you write a simple foreach over some compile-time tuple (like in this case), it's unrolled at compile time similarly to "static foreach", the main difference is whether it creates a sub-scope for the loop body or not. "foreach" creates one, "static foreach" doesn't.
Ah makes sense. Thanks!
|
Copyright © 1999-2021 by the D Language Foundation