toString not working... (a bug in D?)

Daniel Giddings dgiddings at bigworldtech.com
Tue Jan 16 15:17:52 PST 2007


I suppose I should give an example of the matching error:

import std.stdio;
import std.string;

struct S(T...)
{
	static assert( T.length > 0 );
	
	T fields;
	
	char[] toString()
	{
		char[] result = "(" ~ .toString(fields[0]);
		
		foreach( f; fields[1..$] )
			result ~= "," ~ .toString(f);
		
		return result ~ ")";
	}
}
void main()
{
	S!(char[],double) t;
	
	writefln( t );
}

compiling gives:
t.d(12): function std.string.toString (bool) does not match parameter 
types (char[])
t.d(8): Error: cannot implicitly convert expression 
this._fields_field_0) of type char[] to char*
t.d(22): template instance t.S!(char[],double) error instantiating


but using format:

import std.stdio;
import std.string;

struct S(T...)
{
	static assert( T.length > 0 );
	
	T fields;
	
	char[] toString()
	{
		char[] result = "(" ~ format(fields[0]);
		
		foreach( f; fields[1..$] )
			result ~= "," ~ format(f);
		
		return result ~ ")";
	}
}
void main()
{
	S!(char[],double) t;
	
	writefln( t );
}

compiles, and correctly gives:
(,nan)



More information about the Digitalmars-d mailing list