D:YAML 0.3 released
torhu
no at spam.invalid
Thu Nov 17 06:56:09 PST 2011
On 17.11.2011 15:01, Kiith-Sa wrote:
>
> Thanks for your input. Your example doesn't seem to be significantly
> simpler, but a good point is that the Mark structures don't need to
> be passed. I'm considering this style:
>
> ---
> bool constructMyStructScalar(ref Node node)
> {
> auto parts = node.as!string().split(":");
>
> try
> {
> return MyStruct(to!int(parts[0]), to!int(parts[1]), to!int(parts[2]));
> }
> catch(Exception e)
> {
> throw SomeExceptionType("message: " ~ e.msg); //wrapped by the caller
> }
> }
> ---
>
> This would avoid passing the Marks and force the user to specify an
> error message.
I'm sorry if I wasn't clear. The main simplification was that I don't
have to throw the exception myself. If all the custom tag handlers are
going to throw the same exception, I think you should not have to write
that code over and over again.
But I think your new example is a good idea, if you change it to this:
---
bool constructMyStructScalar(ref Node node)
{
auto parts = node.as!string().split(":");
return MyStruct(to!int(parts[0]), to!int(parts[1]),
to!int(parts[2]));
}
---
The exception throw by to() could be wrapped in a YAMLException or
whatever, that contains the position information. The toString would
then add the ConvException's error message to the standard YAML error.
If you want a custom message, you could just throw a plain Exception.
I supppose wrapping every call in a try/catch block would have a
negative impact on performance, though.
More information about the Digitalmars-d-announce
mailing list