www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - parse int error

reply "Peter Eisenhower" <peter.eisenhower gmail.com> writes:
I am confused as to why I cannot pass the return of the tag 
attribute directly into the parse int.

// This works
  string s = xml.tag.attr["key"];
  int key = parse!int(s);

// Compile error on these
int key = parse!int(xml.tag.attr["key"]);
int key = parse!int(cast(string) cml.tag.attr["key"]);
Oct 29 2013
next sibling parent "Peter Eisenhower" <peter.eisenhower gmail.com> writes:
On Wednesday, 30 October 2013 at 01:02:45 UTC, Peter Eisenhower 
wrote:
 I am confused as to why I cannot pass the return of the tag 
 attribute directly into the parse int.

 // This works
  string s = xml.tag.attr["key"];
  int key = parse!int(s);

 // Compile error on these
 int key = parse!int(xml.tag.attr["key"]);
 int key = parse!int(cast(string) cml.tag.attr["key"]);
This is the compile error Error: template instance std.conv.parse!(int,const(immutable(char)[])) error instantiating
Oct 29 2013
prev sibling next sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Wednesday, October 30, 2013 02:02:40 Peter Eisenhower wrote:
 I am confused as to why I cannot pass the return of the tag
 attribute directly into the parse int.
 
 // This works
 string s = xml.tag.attr["key"];
 int key = parse!int(s);
 
 // Compile error on these
 int key = parse!int(xml.tag.attr["key"]);
 int key = parse!int(cast(string) cml.tag.attr["key"]);
parse takes the string by ref and removes what it's parsing from the string, so it can't be a temporary. It has to be an lvalue. Also, casting results in an rvalue. To use parse, you should generally be using a local variable for the string. - Jonathan M Davis
Oct 29 2013
prev sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 10/29/2013 06:02 PM, Peter Eisenhower wrote:
 I am confused as to why I cannot pass the return of the tag attribute
 directly into the parse int.

 // This works
   string s = xml.tag.attr["key"];
   int key = parse!int(s);

 // Compile error on these
 int key = parse!int(xml.tag.attr["key"]);
 int key = parse!int(cast(string) cml.tag.attr["key"]);
A trivial wrapper makes it convenient: import std.xml; import std.conv; T getAttr(T)(DocumentParser xml, string attrName) { string s = xml.tag.attr[attrName]; T attr = parse!T(s); return attr; } unittest { auto xml = new DocumentParser(`<test key="1"></test>`); auto key = xml.getAttr!int("key"); assert(key == 1); } void main() {} Ali
Oct 30 2013
parent "Peter Eisenhower" <peter.eisenhower gmail.com> writes:
On Wednesday, 30 October 2013 at 18:19:13 UTC, Ali Çehreli wrote:
 On 10/29/2013 06:02 PM, Peter Eisenhower wrote:
 I am confused as to why I cannot pass the return of the tag 
 attribute
 directly into the parse int.

 // This works
  string s = xml.tag.attr["key"];
  int key = parse!int(s);

 // Compile error on these
 int key = parse!int(xml.tag.attr["key"]);
 int key = parse!int(cast(string) cml.tag.attr["key"]);
A trivial wrapper makes it convenient: import std.xml; import std.conv; T getAttr(T)(DocumentParser xml, string attrName) { string s = xml.tag.attr[attrName]; T attr = parse!T(s); return attr; } unittest { auto xml = new DocumentParser(`<test key="1"></test>`); auto key = xml.getAttr!int("key"); assert(key == 1); } void main() {} Ali
Thanks
Nov 03 2013