Thread overview | ||||||
---|---|---|---|---|---|---|
|
March 17, 2013 "this" member for structs with methods | ||||
---|---|---|---|---|
| ||||
When a struct contains methods, __traits(allMembers reports a member called "this". What is "this"? void main() { struct S { int i; } struct A { int i; void f() {} } pragma(msg, __traits(allMembers, S)); // i pragma(msg, __traits(allMembers, A)); // i, f, this } |
March 17, 2013 Re: "this" member for structs with methods | ||||
---|---|---|---|---|
| ||||
Posted in reply to simendsjo | On Sunday, 17 March 2013 at 12:16:08 UTC, simendsjo wrote:
> When a struct contains methods, __traits(allMembers reports a member called "this". What is "this"?
>
> void main() {
> struct S { int i; }
> struct A { int i; void f() {} }
> pragma(msg, __traits(allMembers, S)); // i
> pragma(msg, __traits(allMembers, A)); // i, f, this
> }
This is context pointer. Move outside of function to not to have it.
|
March 17, 2013 Re: "this" member for structs with methods | ||||
---|---|---|---|---|
| ||||
Posted in reply to Maxim Fomin | On Sunday, 17 March 2013 at 13:30:29 UTC, Maxim Fomin wrote: > On Sunday, 17 March 2013 at 12:16:08 UTC, simendsjo wrote: >> When a struct contains methods, __traits(allMembers reports a member called "this". What is "this"? >> >> void main() { >> struct S { int i; } >> struct A { int i; void f() {} } >> pragma(msg, __traits(allMembers, S)); // i >> pragma(msg, __traits(allMembers, A)); // i, f, this >> } > > This is context pointer. Move outside of function to not to have it. Should it be included? The documentation is a bit sparse: http://dlang.org/traits.html#allMembers, but every member listed there are methods in the class or from Object. No hidden this or vtbl shows in the example. |
March 17, 2013 Re: "this" member for structs with methods | ||||
---|---|---|---|---|
| ||||
Posted in reply to simendsjo | On Sunday, 17 March 2013 at 14:27:32 UTC, simendsjo wrote:
> On Sunday, 17 March 2013 at 13:30:29 UTC, Maxim Fomin wrote:
>> On Sunday, 17 March 2013 at 12:16:08 UTC, simendsjo wrote:
>>> When a struct contains methods, __traits(allMembers reports a member called "this". What is "this"?
>>>
>>> void main() {
>>> struct S { int i; }
>>> struct A { int i; void f() {} }
>>> pragma(msg, __traits(allMembers, S)); // i
>>> pragma(msg, __traits(allMembers, A)); // i, f, this
>>> }
>>
>> This is context pointer. Move outside of function to not to have it.
>
> Should it be included? The documentation is a bit sparse: http://dlang.org/traits.html#allMembers, but every member listed there are methods in the class or from Object. No hidden this or vtbl shows in the example.
Yes, it should be documented explicitly. I think it should be included because there is actually a special member which has runtime storage and listing "this" does not make much harm, indeed it can be useful (probably some phobos code depend on this). Vtbl can be accessed anyway and class is reference type, so comparing allMembers for structs and classes is not very good.
|
Copyright © 1999-2021 by the D Language Foundation