[Issue 4716] std.stdio.input() or similar
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Mon Aug 23 05:14:28 PDT 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4716
--- Comment #1 from bearophile_hugs at eml.cc 2010-08-23 05:14:22 PDT ---
Improved code. Both IsType() and IsString() may be useful to add to std.traits,
if something similar is not already present.
import std.string: chomp;
import std.stdio: write, readln;
import std.conv: to;
// true is T is one of the successive types
template IsType(T, Types...) {
static if (Types.length)
enum bool IsType = is(T == Types[0]) || IsType!(T, Types[1..$]);
else
enum bool IsType = true;
}
// true if T is string, wstring, or dstring
template IsString(T) {
enum bool IsString = IsType!(T, string, wstring, dstring);
}
T input(T)(string msg) {
write(msg);
auto read = readln();
static if (IsString!T)
return to!T(read.chomp());
else
return to!T(read.strip());
}
// usage demo --------------
import std.stdio: writeln;
void main() {
//auto x = input!string("Enter value x: ");
auto x = input!double("Enter value x: ");
writeln(">", x, "<");
}
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list