.init property for char[] type
Jeremie Pelletier
jeremiep at gmail.com
Tue Sep 22 05:46:00 PDT 2009
Justin Johansson wrote:
> In a templated class (D1.0) along lines ...
>
> class Foo(T) {
> //..
> static T bar() { return T.init; }
> //..
> }
>
> Foo!(int).bar() returns 0 and Foo!(char[]).bar() returns nil.
>
> I'd much prefer (at least for my purposes) that (char[]).init returned an empty string rather than effectively a null pointer. Is there a convenient solution for this, e.g. by specializing just the bar method of class Foo when T is char[], or by some other means?
>
> Maybe this type of question best be asked on D.learn, but I do wonder if an empty string is a more reasonable initializer for char[] .. well maybe not .. I don't know .. I yield to your sensibilities.
>
> Thanks to all.
>
You could use a custom type, which would solve your .init problem:
typedef string myString = "";
Or you could specialize your bar():
static T bar() {
static if(isSomeString!T)
return "";
else
return T.init;
}
I myself favor a null initializer, since char[] is a reference type, not
a value type, it only makes sense to initialize it to a null reference.
More information about the Digitalmars-d
mailing list