string to thread

armando sano via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Apr 22 17:55:20 PDT 2015


Is there any update on this? This question of the distinction 
between reading/writing to a file stream vs to a string seems 
recurrent.

I am interested in writing to a string and am wondering if there 
is a reason for having to use explicitly the convenience 
functions std.conv.text() (or to!string()) and 
std.string.format() when one would expect to be able to do:

string s;
int n = 10;
s.writeln("Hello ", n, " times"); // s == "Hello 10 times\n"
s.writefln("and %d times", n); // s == "Hello 10 times\nand 10 
times\n"

There is a simple way to emulate this by expanding on string:

struct sstring {
	string _s;
	alias _s this; // treat sstring just like _s, a string
	
	this(string literal = "") { _s = literal; }

	void write(Args...)(Args args) { foreach(a; args)  {_s ~= 
to!string(a);} }
	void writeln(Args...)(Args args) { this.write(args, '\n'); }
	void writef(Char, Args...)(in Char[] fmt, Args args) { _s ~= 
std.string.format(fmt, args); }
	void writefln(Char, Args...)(in Char[] fmt, Args args) { 
this.writef(fmt ~ '\n', args); }	
}

An 'sstring s;' can be then be used just like a normal 'string' 
with the addition of being able to use it as in the example above.

I suspect something similar could be done for reading.

Why the syntactic burden of having to use to!string, 
std.string.format, std.conv.parse etc?


More information about the Digitalmars-d-learn mailing list