Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
May 26, 2014 next!T | ||||
---|---|---|---|---|
| ||||
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 |
May 26, 2014 Re: next!T | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrew Edwards | 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
|
May 26, 2014 Re: next!T | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On 5/25/14, 10:12 PM, Ali Çehreli wrote: > 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. >> > > Works like a charm here! :) > Thanks Ali. |
June 07, 2014 Re: next!T | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On 5/25/14, 10:12 PM, Ali Çehreli wrote: > 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 > So one of the things I really wanted to do is Unicode IO (namely Japanese) from the console. I tried using this and quickly realized that no considerations were taken. The modified version next(T)() now looks like this: auto next(T)() { import std.traits; import std.conv; import std.string; import std.range; if(buffer.length == 0) buffer = stdin.readln; static if (isSomeString!T) { // [1] scope (exit) buffer = null; return buffer.strip.to!T; } static if (isSomeChar!T) { scope (exit) { buffer.popFront; buffer = buffer.strip; } return stride(buffer, 1).array[0].to!T; } else { scope (exit) buffer = buffer.strip; return parse!T(buffer); // [2] Line 64 } } This function now works for all types except dstring. This remains a problem I cannot figure out. The error code is as follows: $ rdmd -unittest textnext /usr/share/dmd/src/phobos/std/conv.d(3293): Error: cannot modify immutable expression c /usr/share/dmd/src/phobos/std/conv.d(3297): Error: cannot modify immutable expression c /usr/share/dmd/src/phobos/std/conv.d(2904): Error: template instance std.conv.parseElement!(immutable(dchar), string) error instantiating io.d(64): instantiated from here: parse!(immutable(dchar)[], string) textnext.d(12): instantiated from here: next!(immutable(dchar)[]) io.d(64): Error: template instance std.conv.parse!(immutable(dchar)[], string) error instantiating textnext.d(12): instantiated from here: next!(immutable(dchar)[]) textnext.d(12): Error: template instance io.next!(immutable(dchar)[]) error instantiating Failed: ["dmd", "-unittest", "-v", "-o-", "textnext.d", "-I."] This error is report failure on dstring at [2] but I fully expected them at [1]. I've looked at implementation of isSomeString() (https://github.com/D-Programming-Language/phobos/blob/master/std/traits.d#L5178) and clearly dstrings are addressed in the implementation there. What exactly am I missing in my understanding? Any assistance/advice is appreciated. Thanks, Andrew |
June 07, 2014 Re: next!T | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrew Edwards | On Saturday, 7 June 2014 at 02:23:18 UTC, Andrew Edwards wrote:
>
> This function now works for all types except dstring. This remains a problem I cannot figure out. The error code is as follows:
>
> $ rdmd -unittest textnext
> /usr/share/dmd/src/phobos/std/conv.d(3293): Error: cannot modify immutable expression c
> /usr/share/dmd/src/phobos/std/conv.d(3297): Error: cannot modify immutable expression c
> /usr/share/dmd/src/phobos/std/conv.d(2904): Error: template instance std.conv.parseElement!(immutable(dchar), string) error instantiating
> io.d(64): instantiated from here: parse!(immutable(dchar)[], string)
> textnext.d(12): instantiated from here: next!(immutable(dchar)[])
> io.d(64): Error: template instance std.conv.parse!(immutable(dchar)[], string) error instantiating
> textnext.d(12): instantiated from here: next!(immutable(dchar)[])
> textnext.d(12): Error: template instance io.next!(immutable(dchar)[]) error instantiating
> Failed: ["dmd", "-unittest", "-v", "-o-", "textnext.d", "-I."]
>
> This error is report failure on dstring at [2] but I fully expected them at [1].
>
> I've looked at implementation of isSomeString() (https://github.com/D-Programming-Language/phobos/blob/master/std/traits.d#L5178) and clearly dstrings are addressed in the implementation there. What exactly am I missing in my understanding?
>
> Any assistance/advice is appreciated.
>
> Thanks,
> Andrew
I got it working here by putting an else before the second static-if.
|
June 07, 2014 Re: next!T | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Nicholson-Sauls | On 6/6/14, 10:57 PM, Chris Nicholson-Sauls wrote:
> On Saturday, 7 June 2014 at 02:23:18 UTC, Andrew Edwards wrote:
>>
>> Any assistance/advice is appreciated.
>>
>> Thanks,
>> Andrew
>
>
> I got it working here by putting an else before the second static-if.
Wow. Sometimes you really cannot see the things you type no matter how long you stare at it. Thank you soo much.
|
June 07, 2014 Re: next!T | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrew Edwards | On Saturday, 7 June 2014 at 03:21:49 UTC, Andrew Edwards wrote:
>
> Wow. Sometimes you really cannot see the things you type no matter how long you stare at it. Thank you soo much.
No problem. I only noticed when I re-typed it by hand to study the flow, and instinctively added the else out of habit and wondered why I was getting no errors. :) Started thinking it was one of those "this is an error, but only on Thursdays" kind of things.
Anyway, since the bugged code was freaking out in the second, unreachable, return statement, it leads me to thinking whether we could provide some sane way for the compiler to point out such things (unreachable returns). It would have made this case, and ones like it, more obvious.
|
Copyright © 1999-2021 by the D Language Foundation