On Thursday, 19 September 2019 at 19:49:02 UTC, divi wrote:
Due to prior design choices, I have the following setup:

enum K { A, B }

mixin template Magic()
{
  mixin(`alias Other = ` ~ magic() ~ `!(K.B);`);
}

Just how constrained are you? I'd be inclined to completely remove the magic() function and make it something like this:

mixin template Magic()
{
  static if(is(typeof(this) == T!(A), alias T, A...))
        alias Other = T!(K.B);
  else
        static assert(0);
}


But then you also need to change the constraint to something more along these lines:


struct S(K k)
{
  static if (k == K.A)
    mixin Magic; // now makes S!(K.B)
}


That is, moving the if from a constraint outside to a static if inside. The reason for this is kinda crazy: the token `S` inside that struct (and thus inside its template mixin) refers to the *current instantiation*. Which includes the conflicting constraint.

I don't know if there's a way around that. I think it is a minor design flaw in the language personally and would love to see it changed... but I don't know if you can work around it directly, even with mixin strings and external helpers, since the constraint is even copied in the .stringof! Well, I guess you could slice the string on the first " " and cut that stuff off and maybe convince the compiler to look up the template at top level again. That'd probably work but is liable to maybe getting lost in aliases.

If changing that constraint is possible though you can get a reasonably robust solution.

Reply via email to