Question about std.conv.parse

Jonathan M Davis newsgroup.d at jmdavisprog.com
Tue Jan 30 09:29:22 UTC 2018


On Tuesday, January 30, 2018 09:19:22 Jacky via Digitalmars-d-learn wrote:
> Hello everyone.I'm a newbie on the D language.When i use the
> library 'std.conv' ,i met some problem.
> This is what I have:
>
>      static import std.conv;
>      string aaa = "123456789";
>      uint idx = 5;
>      string bbb = aaa[0 .. idx];
>
>      uint work = std.conv.parse!(uint)(bbb); // this works
>
>      uint didnotwork = std.conv.parse!(uint)(aaa[0 .. idx]);
> //but here's a error
>      //template std.conv.parse cannot deduce function from
> argument types !(uint)(string)
>
> So my questions are:
> 1) What is the difference between these two lines?

The first one passes an lvalue. The second one passes an rvalue. parse takes
its argument by ref so that what is parsed is removed from the input. As
such, it requires an lvalue.

> 2) How to correct the second without assign a new variable?

You don't. parse requires that you pass it a variable.

std.conv.to does not take its argument by ref, so you can use that instead,
but it converts the entire argument instead of just the front portion that
matches the requested type. So, if you're trying to convert the entire
argument, then you can use to, but if you're trying to just convert the
front, then you have to use parse, and that means passing a variable.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list