digitalmars.D.learn - Baffled by compilation error for formattedRead
- PhilipDaniels (16/16) May 07 2015 Given a string
- PhilipDaniels (11/11) May 07 2015 On Thursday, 7 May 2015 at 23:10:26 UTC, PhilipDaniels wrote:
- Dennis Ritchie (22/34) May 07 2015 Alternatively, I can suggest to use the function csvReader():
- Justin Whear (6/9) May 07 2015 formattedRead takes its input by ref and consumes it. Your first two
- PhilipDaniels (4/13) May 07 2015 Ok, thanks, I see. It looks a little weird, but there is the same
Given a string string input = "rgb:20/30/40"; And the following: ubyte r, g, b; auto numRead = formattedRead(dropExactly(input, 4), "%x/%x/%x", &r, &g, &b); // does not compile auto numRead = formattedRead(input[4..$], "%x/%x/%x", &r, &g, &b); // does not compile string s2 = input[4..$]; auto numRead = formattedRead(s2, "%x/%x/%x", &r, &g, &b); // compiles Why do the first two fail to compile but the last one does?! I cannot see any difference between the 's2' case and the second case, it is a completely mechanical source code transformation I have made.
May 07 2015
On Thursday, 7 May 2015 at 23:10:26 UTC, PhilipDaniels wrote: Let's try reformatting that... ubyte r, g, b; // does not compile auto numRead = formattedRead(dropExactly(input, 4), "%x/%x/%x", &r, &g, &b); // does not compile auto numRead = formattedRead(input[4..$], "%x/%x/%x", &r, &g, &b); // compiles string s2 = input[4..$]; auto numRead = formattedRead(s2, "%x/%x/%x", &r, &g, &b);
May 07 2015
On Thursday, 7 May 2015 at 23:13:41 UTC, PhilipDaniels wrote:On Thursday, 7 May 2015 at 23:10:26 UTC, PhilipDaniels wrote: Let's try reformatting that... ubyte r, g, b; // does not compile auto numRead = formattedRead(dropExactly(input, 4), "%x/%x/%x", &r, &g, &b); // does not compile auto numRead = formattedRead(input[4..$], "%x/%x/%x", &r, &g, &b); // compiles string s2 = input[4..$]; auto numRead = formattedRead(s2, "%x/%x/%x", &r, &g, &b);Alternatively, I can suggest to use the function csvReader(): ----- import std.csv, std.stdio, std.format; void main() { auto input = "rgb:10/30/40"; input = input[4 .. $]; ubyte r, g, b; formattedRead(input, "%s/%s/%s", &r, &g, &b); auto numRead = [r, g, b]; string[] numReadHex; foreach (e; numRead) { numReadHex ~= format("%x", e); } writeln(numRead); // [10, 30, 40] writeln(numReadHex); // ["a", "1e", "28"] auto inputNew = "rgb:10/30/40"; auto newNumRead = csvReader!int(inputNew[4 .. $], '/').front; writeln(newNumRead); // [10, 30, 40] }
May 07 2015
On Thu, 07 May 2015 23:10:26 +0000, PhilipDaniels wrote:Why do the first two fail to compile but the last one does?! I cannot see any difference between the 's2' case and the second case, it is a completely mechanical source code transformation I have made.formattedRead takes its input by ref and consumes it. Your first two attempts are both passing the result of functions (dropExactly and opSlice) which are temporary rvalues and can thus not be passed by reference. Here's more reading on the subject of rvalues: http:// ddili.org/ders/d.en/lvalue_rvalue.html
May 07 2015
On Thursday, 7 May 2015 at 23:23:08 UTC, Justin Whear wrote:formattedRead takes its input by ref and consumes it. Your first two attempts are both passing the result of functions (dropExactly and opSlice) which are temporary rvalues and can thus not be passed by reference. Here's more reading on the subject of rvalues: http:// ddili.org/ders/d.en/lvalue_rvalue.htmlOk, thanks, I see. It looks a little weird, but there is the same signatures more closely...My excuse is it's 1 am :-)
May 07 2015