parse int error

Ali Çehreli acehreli at yahoo.com
Wed Oct 30 11:19:12 PDT 2013


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



More information about the Digitalmars-d-learn mailing list