<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">2013/9/20 Andrei Alexandrescu <span dir="ltr"><<a href="mailto:SeeWebsiteForEmail@erdani.org" target="_blank">SeeWebsiteForEmail@erdani.org</a>></span><br>
<blockquote class="gmail_quote" style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Consider a struct that may or may not have state depending on a type parameter:<br>

<br>
struct S(T)<br>
{<br>
  enum hasState = FieldTypeTuple!T.length || isNested!T;<br>
  static if (hasState)<br>
    T _theT;<br>
  else<br>
    alias _theT = T;<br>
  ...<br>
}<br>
<br>
This is really nice because I don't bloat S unnecessarily and I get to use _theT.method() uniformly whether or not it's the type itself or the data member.<br>
<br>
The duplication problem appears when S itself must define a method that should be static or nonstatic depending on the existence of state. Consider:<br>
<br>
struct S(T)<br>
{<br>
  ... continued from above ...<br>
  if (hasState)<br>
    int method() { return 1 + _theT.method(); }<br>
  else<br>
    static int method() { return 1 + _theT.method(); }<br>
}<br>
<br>
In the general case the body of S!T.method() may be of course larger, which makes for a nasty duplication - essentially all but the "static" keyword must be duplicated.<br>
<br>
Any ideas for a clean solution? I can't get much further than string mixins, which wouldn't be clean :o).<br></blockquote><div><br></div>Just an idea: string mixin + UDA?<div><br></div><div><div>  @mixin(hasState ? "" : "static")</div>
<div>  int method() { return 1 + _theT.method(); }</div></div><div><br></div><div>Kenji Hara</div></div></div></div>