digitalmars.D.learn - Question about std.conv.parse
- Jacky (17/17) Jan 30 2018 Hello everyone.I'm a newbie on the D language.When i use the
- Jonathan M Davis (11/26) Jan 30 2018 The first one passes an lvalue. The second one passes an rvalue. parse t...
- Jacky (4/19) Jan 30 2018 Thank you very much!I think i should dig deeper into this
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 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
Jan 30 2018
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:Thank you very much!I think i should dig deeper into this interesting language. :)[...]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
Jan 30 2018