www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - class templates and static if

--f46d04440266caca0e04b9e64a95
Content-Type: text/plain; charset=ISO-8859-1

So, here's my code, as it stands currently:

import std.stdio;

static enum Type {
request,
 response
};

class Parser(Type t) {
static if (t == Type.request) {
 string name = "request";
} else {
string name = "response";
 }

string message;

this(string message) {
 this.message = message;
}
}

void main() {
 immutable Type t = Type.request;
Parser!t h = new Parser!t("Hello world");

writefln("%s: %s", h.name, h.message);
}

The general goal is to make a templated class that will only include the
parts that each needs. I would like to keep this all as one class template,
because there is a lot of shared code, and I would like to keep it as
seamless as possible and not have to separate out code into separate
functions.

Anyway, my current approach is a little clunky, because it requires an
immutable to be created just to tell the compiler what type of parser I
need.

I was thinking about having two classes, Request and Response, and have
parser take a normal template (class Parser (T)), but I wanted to restrict
it to just those two classes (for now), so I came up with the enum solution.

Is there a better way to do this?

What I'd ultimately like to do is have some static if surrounding blocks of
code that depends on the input to the template.

--f46d04440266caca0e04b9e64a95
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div>So, here&#39;s my code, as it stands currently:</div><div><br></div><d=
iv>import std.stdio;</div><div><br></div><div>static enum Type {</div><div>=
<span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>request,</=
div>

<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>respo=
nse</div><div>};</div><div><br></div><div>class Parser(Type t) {</div><div>=
<span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>static if =
(t =3D=3D Type.request) {</div>

<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">		</span>stri=
ng name =3D &quot;request&quot;;</div><div><span class=3D"Apple-tab-span" s=
tyle=3D"white-space:pre">	</span>} else {</div><div><span class=3D"Apple-ta=
b-span" style=3D"white-space:pre">		</span>string name =3D &quot;response&q=
uot;;</div>

<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>}</di=
v><div><br></div><div><span class=3D"Apple-tab-span" style=3D"white-space:p=
re">	</span>string message;</div><div><br></div><div><span class=3D"Apple-t=
ab-span" style=3D"white-space:pre">	</span>this(string message) {</div>

<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">		</span>this=
.message =3D message;</div><div><span class=3D"Apple-tab-span" style=3D"whi=
te-space:pre">	</span>}</div><div>}</div><div><br></div><div>void main() {<=
/div>
<div>
<span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>immutable =
Type t =3D Type.request;</div><div><span class=3D"Apple-tab-span" style=3D"=
white-space:pre">	</span>Parser!t h =3D new Parser!t(&quot;Hello world&quot=
;);</div>

<div><br></div><div><span class=3D"Apple-tab-span" style=3D"white-space:pre=
">	</span>writefln(&quot;%s: %s&quot;, <a href=3D"http://h.name">h.name</a>=
, h.message);</div><div>}</div><div><br></div><div>The general goal is to m=
ake a templated class that will only include the parts that each needs. I w=
ould like to keep this all as one class template, because there is a lot of=
 shared code, and I would like to keep it as seamless as possible and not h=
ave to separate out code into separate functions.</div>

<div><br></div><div>Anyway, my current approach is a little clunky, because=
 it requires an immutable to be created just to tell the compiler what type=
 of parser I need.</div><div><br></div><div>I was thinking about having two=
 classes, Request and Response, and have parser take a normal template (cla=
ss Parser (T)), but I wanted to restrict it to just those two classes (for =
now), so I came up with the enum solution.</div>

<div><br></div><div>Is there a better way to do this?</div><div><br></div><=
div>What I&#39;d ultimately like to do is have some static if surrounding b=
locks of code that depends on the input to the template.</div>

--f46d04440266caca0e04b9e64a95--
Feb 26 2012