next!T

Andrew Edwards via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun May 25 17:21:12 PDT 2014


Hello All,

I wrote the following convenience functions to aid in my studies. 
Unfortunately, I'm using Java books (Ali I will get to yours soon 
enough) so the need was necessitated by the frequency of use in the 
examples. Would appreciate a sanity check. Is there something that I 
should be thinking about am obviously not? Comments/criticisms appreciated.

------------ io.d ------------
module io;

public import std.stdio;

private string buffer;

auto next(T)()
{
	import std.traits;
	import std.conv;
	import std.string;

	if(buffer.length == 0)
		buffer = stdin.readln;

	static if (isSomeString!T) {
		scope (exit) buffer = null;
		return buffer.strip;
	}
	else {
		scope (exit) buffer = buffer.strip;
		return parse!T(buffer);
	}
}

auto next(T)(string msg)
{
	if (msg != null)
		msg.write;

	return next!T;
}
------------ End ------------

Thanks,
Andrew


More information about the Digitalmars-d-learn mailing list