www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Why does formattedRead take a non-const ref?

reply "Andrew Godfrey" <X y.com> writes:
The first parameter of formattedRead is a non-const ref. Is there
a good reason for this?

e.g. the below doesn't compile, but if I remove the 'const' from 
Foo.normalize, then it succeeds:

unittest {
     import std.datetime;
     struct Foo {
         string date;
         DateTime normalize() const {
             import std.format, std.exception;
             int month, day, year;
             enforce(3 == formattedRead(date, "%d/%d/%d", &month, 
&day, &year));
             return DateTime(year, month, day, 0, 0, 0);
         }
     }

     Foo foo = Foo("12/2/2014");
     assert(foo.normalize == DateTime(2014, 12, 2, 0, 0, 0));
}
Aug 28 2014
next sibling parent reply "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Friday, 29 August 2014 at 04:21:54 UTC, Andrew Godfrey wrote:
 The first parameter of formattedRead is a non-const ref. Is 
 there
 a good reason for this?
formattedRead takes an input range as the first parameter, and consumes it as it is going through the format string. On exit, the range will contain the remainder of the initial range after all fields have been read and parsed.
Aug 28 2014
parent "Andrew Godfrey" <X y.com> writes:
On Friday, 29 August 2014 at 04:29:31 UTC, Vladimir Panteleev 
wrote:
 On Friday, 29 August 2014 at 04:21:54 UTC, Andrew Godfrey wrote:
 The first parameter of formattedRead is a non-const ref. Is 
 there
 a good reason for this?
formattedRead takes an input range as the first parameter, and consumes it as it is going through the format string. On exit, the range will contain the remainder of the initial range after all fields have been read and parsed.
Ah, thanks! I should have posted this in D.learn, sorry.
Aug 28 2014
prev sibling parent "Dicebot" <public dicebot.lv> writes:
const(char)[] tmp = date;
enforce(3 == formattedRead(tmp, "%d/%d/%d", &month,
&day, &year));
Aug 28 2014