www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Baffled by compilation error for formattedRead

reply "PhilipDaniels" <phil foo.com> writes:
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
next sibling parent reply "PhilipDaniels" <phil foo.com> writes:
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
parent "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
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
prev sibling parent reply Justin Whear <justin economicmodeling.com> writes:
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
parent "PhilipDaniels" <phil foo.com> writes:
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.html
Ok, 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