The Right Approach to Exceptions

foobar foo at bar.com
Mon Feb 20 12:01:27 PST 2012


On Monday, 20 February 2012 at 19:41:07 UTC, Nick Sabalausky 
wrote:
> Ok, I just saw your explanation here:
> http://forum.dlang.org/post/jhu1hk$1cb8$1@digitalmars.com
>
> And, IIUC, I assume that inside "stringTemplate()", you'd then 
> access some
> table that roughly amounts to (yea, I know we don't have 2D 
> AAs):
>
> string[string, Locale] i18nTable;
> i18nTable["FileNotFoundExcepton", English] = "File ${filename} 
> not found";
>
> Then stringTemplate() would look that up, find the string "File 
> ${filename}
> not found" and essentially do:
>
> return "File "~e.info("filename")~" not found";
>
> Although obviously not hardcoded like that.
>
> Is that right?
>
> In that case, I like the general idea, *but* why not just use 
> reflection to
> access the members instead of essentially creating a JS-style 
> "class" with
> AAs? That way we don't have to either A. throw away the 
> benefits that class
> members have over AAs or B. violate DRY by duplicating field 
> members in an
> AA.

Seconded. Reflection seems a much better solution for this.
The i18n table would map (exception type) -> (language, format 
string)
and the i18n formatter would use reflection to map field values 
to the format string.
a format string would be e.g. "File {filename} not found" and the 
formatter would replace {filename} with 
e.class.getField("filename").

This does not require any modifications to the exception 
mechanism.


More information about the Digitalmars-d mailing list