Thread overview | ||||||
---|---|---|---|---|---|---|
|
December 06, 2017 Parsing a string from stdin using formattedRead | ||||
---|---|---|---|---|
| ||||
std.format has the function formattedRead which can be used to parse a string, e.g. string first_name; string last_name; string info = readln(); formattedRead(info, "%s %s", first_name, last_name); This piece of code works as intended. However, since I don't need the input after it's parsed, I wanted to omit the 3rd line of code above and replace the 4th line with formattedRead(readln(), "%s %s", first_name, last_name); But this raises a compilation error, claiming that formattedRead "cannot deduce function from argument types". What's the problem here? |
December 06, 2017 Re: Parsing a string from stdin using formattedRead | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mark | On 12/06/2017 10:47 AM, Mark wrote: > string info = readln(); > formattedRead(info, "%s %s", first_name, last_name); > > This piece of code works > formattedRead(readln(), "%s %s", first_name, last_name); > > But this raises a compilation error, claiming that formattedRead "cannot > deduce function from argument types". formattedRead takes the first parameter as 'ref'. I hope we can change compiler error messages like this. What it means is that although the 'string' part of the argument matched, it cannot be passed as 'ref' because what readln() returns is an rvalue. (On the other hand, info above is an lvalue.) rvalues cannot be bound to references in D. Ali |
December 06, 2017 Re: Parsing a string from stdin using formattedRead | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Wednesday, 6 December 2017 at 18:57:55 UTC, Ali Çehreli wrote:
> On 12/06/2017 10:47 AM, Mark wrote:
>
> > string info = readln();
> > formattedRead(info, "%s %s", first_name, last_name);
> >
> > This piece of code works
>
> > formattedRead(readln(), "%s %s", first_name, last_name);
> >
> > But this raises a compilation error, claiming that
> formattedRead "cannot
> > deduce function from argument types".
>
> formattedRead takes the first parameter as 'ref'.
>
> I hope we can change compiler error messages like this. What it means is that although the 'string' part of the argument matched, it cannot be passed as 'ref' because what readln() returns is an rvalue. (On the other hand, info above is an lvalue.) rvalues cannot be bound to references in D.
>
> Ali
I see. That's somewhat inconvenient.
I recall there was some debate(s) about allowing rvalues as const references (like in C++). What was the verdict on that?
|
December 07, 2017 Re: Parsing a string from stdin using formattedRead | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mark | On Wednesday, 6 December 2017 at 18:47:03 UTC, Mark wrote:
> formattedRead(readln(), "%s %s", first_name, last_name);
You could write a little wrapper function to do that
uint myformattedRead (R, Char, S...)(R r, const(Char)[] fmt, auto ref S args) {
formattedRead(r, fmt, args);
}
It just strips the ref off of the input string but otherwise forwards the same thing.
The reason it is ref tho btw is that that allows it to continue off a particular input; you can do like
string s;
formattedRead(s, ....)
formattedRead(s, ....) // this one picks up where the last one left off
|
Copyright © 1999-2021 by the D Language Foundation