www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Question about std.conv.parse

reply Jacky <Jaackky yopmail.com> writes:
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?
2) How to correct the second without assign a new variable?

Cheers,
Jacky
Jan 30
parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
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
Jan 30
parent Jacky <jacky yopmail.com> writes:
On Tuesday, 30 January 2018 at 09:29:22 UTC, Jonathan M Davis 
wrote:
 On Tuesday, January 30, 2018 09:19:22 Jacky via 
 Digitalmars-d-learn wrote:
 [...]
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.
 [...]
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
Thank you very much!I think i should dig deeper into this interesting language. :)
Jan 30