MixinConstraints

Mixes in constraints into another mixin template, to provide static guarantees that it is not mixed into a type of scope other than the one specified.

Using this you can ensure that a mixin template meant to be mixed into a class isn't mixed into a module-level scope, or into a function, etc.

More than one scope type can be supplied with bitwise OR.

mixin template MixinConstraints (
MixinScope mixinScope
string mixinName = "a constrained mixin"
) {}

Parameters

mixinScope

The scope into which to only allow the mixin to be mixed in. All other kinds of scopes will be statically rejected.

mixinName

Optional string name of the mixing-in mixin. Can be anything; it's just used for the error messages.

Examples

module foo;

mixin template Foo()
{
    mixin MixinConstraints!(MixinScope.module_, "Foo");  // Constrained to module-level scope
}

mixin Foo;  // no problem, scope is MixinScope.module_

void bar()
{
    mixin Foo;  // static assert(0): scope is MixinScope.function_, not MixinScope.module_
}

class C
{
    mixin Foo;  // static assert(0): ditto but MixinScope.class_
}

struct C
{
    mixin Foo;  // static assert(0): ditto but MixinScope.struct_
}

mixin template FooStructOrClass()
{
    mixin MixinConstraints(MixinScope.struct_ | MixinScope.class_);
}
void fun()
{
    // MixinConstraints!(MixinScope.function_, "TestMixinConstrainedToFunctions");
    mixin TestMixinConstrainedToFunctions;
}

class TestClassC
{
    // MixinConstraints!(MixinScope.class_, "TestMixinConstrainedToClass");
    mixin TestMixinConstrainedToClass;
}

struct TestStructS
{
    // mixin MixinConstraints!(MixinScope.struct_, "TestMixinConstrainedToStruct");
    mixin TestMixinConstrainedToStruct;
}

struct TestStructS2
{
    mixin TestMixinConstrainedToClassOrStruct;
}

Meta