Thanks for that bearophile - I'll get myself subscribed right away. Bye for now,
Luke On Sun, 23 Jan 2011 09:17:05 -0500, "bearophile" <bearophileh...@lycos.com> said: > Luke J. West: > > > Hi, > > > > I want to specialize a template function - call it print() - for three > > cases: classes, structs and arrays. Ideally I'd like something that > > looks 'functional' like a proper specialization, but perhaps I need to > > use "static if". I'm still at the beginning of my journey with D so I'd > > be grateful for any pointers (sorry - that's a terrible pun). Perhaps > > there's a corner of the D2 docs I've glossed over. > > I suggest you to ask such questions in the D.learn newsgroup. > Here are two possible implementations: > > import std.stdio: writeln; > import std.traits: isArray; > > void print(T)(T x) if (!isArray!T && !is(T == class) && !is(T == struct)) > { > writeln("general"); > } > > void print(T)(T[] x) { > writeln("Dynamic array or fixed-sized array"); > } > > void print(T)(T x) if (is(T == class)) { > writeln("Class instance"); > } > > void print(T)(T x) if (is(T == struct)) { > writeln("Struct instance"); > } > > void print2(T)(T x) { > static if (isArray!T) > writeln("Dynamic array or fixed-sized array"); > else static if (is(T == class)) > writeln("Class instance"); > else static if (is(T == struct)) > writeln("Struct instance"); > else > writeln("general"); > } > > > class C {} > struct S {} > > void main() { > C c; // some class > S s; // some struct > int[4] a; // a fixed-sized array > int p; // a primitive type > > print(a); > print(p); > print(c); > print(s); > writeln(); > > print2(a); > print2(p); > print2(c); > print2(s); > } > > Bye, > bearophile