template specialization
Chris Nicholson-Sauls
ibisbasenji at gmail.com
Sun Mar 4 10:48:00 PST 2007
Thorsten Kiefer wrote:
> OK, I solved it that way :
>
> char[] toString(T)(T x){
> static if(is(T B == B[])) {
> char[] s;
> s ~= '[';
> if(x.length > 0){
> s ~= toString(x[0]);
> foreach(y;x[1..$]){
> s ~= ',';
> s ~= toString(y);
> }
> }
> s ~= ']';
> return s;
> } else {
> return std.string.toString(x);
> }
> }
>
You could also get this same effect just by calling std.string.format(), which as I recall
formats arrays in precisely this way.
format("%s", [1, 2, 3]) => "[1, 2, 3]"
(Admittedly its been a little while since I used Phobos, though.)
-- Chris Nicholson-Sauls
More information about the Digitalmars-d
mailing list