From c00e0fa7e62444a04dbb7362eeba4f906e94b99a Mon Sep 17 00:00:00 2001 From: Jeremy Baxter Date: Tue, 18 Jun 2024 17:42:36 +1200 Subject: [PATCH] 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. --- initial.d | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/initial.d b/initial.d index 1181b9f..289c33c 100644 --- a/initial.d +++ b/initial.d @@ -234,10 +234,10 @@ struct INISection + with a message containing location information. +/ T - keyAs(T)(string k, string defaultValue = null) + keyAs(T)(string k, T defaultValue) { try - return key(k, defaultValue).to!T(); + return k in keys ? keys[k].to!T() : defaultValue; catch (ConvException) throw new INITypeException( format!"unable to convert [%s].%s to %s"(name, k, T.stringof));