next!T
Ali Çehreli via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun May 25 19:12:53 PDT 2014
On 05/25/2014 05:21 PM, Andrew Edwards wrote:
> 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
Works like a charm here! :)
void main()
{
auto s = next!string("What is your name? ");
auto i = next!uint("Your age? ");
auto a = next!string("Your address? ");
auto arr = next!(string[])("Names of your children? ");
writefln("%s year old %s lives in %s. Children: %s", i, s, a, arr);
}
Ali
More information about the Digitalmars-d-learn
mailing list