Thread overview | ||||||
---|---|---|---|---|---|---|
|
September 19, 2012 access enclosing type from shared static this() | ||||
---|---|---|---|---|
| ||||
I want to access the type of the enclosing struct in in a shared static initializer.. How do I do that? The following code will not compile: mixin template MsgMixin(T ...) { static string getName(this M)() { return M.stringof; } shared static this() { import std.stdio; writeln("register " ~ getName()); } } struct MsgTestMsg { mixin MsgMixin!(); } I get the following error messages: src/main.d(40): Error: template main.MsgTestMsg.MsgMixin!().getName does not match any function template declaration src/main.d(40): Error: template main.MsgTestMsg.MsgMixin!().getName(this M) cannot deduce template function from argument types !()() If not possible, just getting the name of the enclosing struct would help a lot! -Øivind |
September 19, 2012 Re: access enclosing type from shared static this() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Øivind | > If not possible, just getting the name of the enclosing struct would
> help a lot!
>
> -Øivind
typeof(this).stringof
This should do it
|
September 19, 2012 Re: access enclosing type from shared static this() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Øivind | On 09/19/2012 09:37 PM, "Øivind" wrote:
> I want to access the type of the enclosing struct in in a shared static
> initializer.. How do I do that? The following code will not compile:
>
> mixin template MsgMixin(T ...) {
> static string getName(this M)() {
> return M.stringof;
> }
> shared static this() {
> import std.stdio;
> writeln("register " ~ getName());
> }
> }
>
> struct MsgTestMsg {
> mixin MsgMixin!();
> }
>
> I get the following error messages:
>
> src/main.d(40): Error: template main.MsgTestMsg.MsgMixin!().getName does
> not match any function template declaration
> src/main.d(40): Error: template main.MsgTestMsg.MsgMixin!().getName(this
> M) cannot deduce template function from argument types !()()
>
> If not possible, just getting the name of the enclosing struct would
> help a lot!
>
> -Øivind
>
use typeof(this), eg:
mixin template MsgMixin(T ...) {
shared static this() {
import std.stdio;
writeln("register " ~ typeof(this).stringof);
}
}
|
September 19, 2012 Re: access enclosing type from shared static this() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | Thanks a lot both of you. The code below worked. I did not expect 'this' to be available in the static function, but of course the type of 'this' is available.
> mixin template MsgMixin(T ...) {
> shared static this() {
> import std.stdio;
> writeln("register " ~ typeof(this).stringof);
> }
> }
|
Copyright © 1999-2021 by the D Language Foundation