avoid converting objects twice

If I have an object of type T and want to set it as the default
value for a call to .keyAs(), I have to convert it to a string first.
This string would then get converted again in case the key doesn't exist
in the section.
This commit is contained in:
Jeremy Baxter 2024-06-18 17:42:36 +12:00
parent baafe10b17
commit c00e0fa7e6

View file

@ -234,10 +234,10 @@ struct INISection
+ with a message containing location information. + with a message containing location information.
+/ +/
T T
keyAs(T)(string k, string defaultValue = null) keyAs(T)(string k, T defaultValue)
{ {
try try
return key(k, defaultValue).to!T(); return k in keys ? keys[k].to!T() : defaultValue;
catch (ConvException) catch (ConvException)
throw new INITypeException( throw new INITypeException(
format!"unable to convert [%s].%s to %s"(name, k, T.stringof)); format!"unable to convert [%s].%s to %s"(name, k, T.stringof));