print function

ixid via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Feb 4 06:02:04 PST 2016


On Thursday, 4 February 2016 at 13:46:46 UTC, Dejan Lekic wrote:
> On Thursday, 4 February 2016 at 00:23:07 UTC, ixid wrote:
>> It would be nice to have a simple writeln that adds spaces 
>> automatically like Python's 'print' in std.stdio, perhaps 
>> called print.
>
> There are many implementations of string interpolation in D 
> (that is what you want, basically). One of them is given in 
> Phillipe's excellent book about templates: 
> https://github.com/PhilippeSigaud/D-templates-tutorial/blob/master/D-templates-tutorial.md#simple-string-interpolation .

I have written an attempt at it but my point was that a print 
function would be a good addition to the standard library rather 
than asking someone to write an implementation for me.

string makePrintString(T)(T length) {
	import std.conv : to;
		
	string s = "writeln(";
	foreach( i; 0 .. length) {
		s ~= "a[" ~ i.to!string ~ "]";
		if(i != length - 1)
			s ~= ",\" \",";
		else s ~= ");";
	}
			
	return s;
}	

void print(A...)(A a) {		
	static if(a.length) {
		mixin(makePrintString(a.length));
	} else writeln;
}


More information about the Digitalmars-d-learn mailing list