Get the type that a mixin is mixing into

John Colvin john.loughran.colvin at gmail.com
Mon Mar 17 07:14:19 PDT 2014


On Monday, 17 March 2014 at 13:04:36 UTC, Mike wrote:
> Hello,
>
> Consider the following silly, but illustrative example:
> ***********************************************
> mixin template HasValue(ContainingType)
> {
>     uint value = ContainingType.outerValue;
> }
>
> struct MyStruct
> {
>     uint outerValue;
>
>     mixin HasValue!(typeof(this));
> }
> ***********************************************
>
>
> Now what if I wanted to use that mixin in a module:
> ***********************************************
> module myModule;
>
> uint outerValue;
>
> mixin HasValue!(typeof({what?});   //how do I refer to the 
> module type
>                                      there is no 'this' for 
> modules
> ************************************************
>
> Basic question:  How can I have my mixin refer to the type it's 
> mixing into, and have it work regardless of whether it's mixing 
> into a module or a class/struct?
>
> Thanks for the help,
> Mike

That doesn't work in either case: you can't initialise a member 
of a mixin template with a runtime value.

To refer to the module name, just use the name of the module. 
Here's something that does work, to demonstrate that:

module myModule;

mixin template HasValue(alias ContainingType)
{
     alias value = ContainingType.outerValue;
}

struct foo
{
     uint outerValue;

     mixin HasValue!(typeof(this));
}

uint outerValue;

mixin HasValue!(myModule);


More information about the Digitalmars-d-learn mailing list