Thread overview
Baffled by compilation error for formattedRead
May 07, 2015
PhilipDaniels
May 07, 2015
PhilipDaniels
May 08, 2015
Dennis Ritchie
May 07, 2015
Justin Whear
May 07, 2015
PhilipDaniels
May 07, 2015
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 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.html

Ok, thanks, I see. It looks a little weird, but there is the same restriction in C#, actually. Must learn to read those D function signatures more closely...My excuse is it's 1 am :-)
May 08, 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]
}