collectExceptionMsg and emptyExceptionMsg
Jonathan M Davis
jmdavisProg at gmx.com
Wed Apr 18 11:13:55 PDT 2012
On Wednesday, April 18, 2012 13:52:30 Jonathan M Davis wrote:
> On Wednesday, April 18, 2012 19:32:44 Andrej Mitrovic wrote:
> > On 4/18/12, Jonathan M Davis <jmdavisProg at gmx.com> wrote:
> > > Probably to avoid any issues of null vs empty
> >
> > But what I'm saying is an empty string isn't null.
>
> True. But by avoiding the empty string, the entire issue of null == "" is
> avoided.
>
> > > It also makes it very clear
> > > that you got an empty exception message when printing the message,
> > > whereas
> > > there won't be any difference between null and empty when you do
> > > something
> > > like
> > >
> > > writeln(collectExceptionMessage(foo()));
> >
> > Well typically you would write the file and line number, or use
> > specific formatting such as writefln('%s', str).
>
> Which wouldn't help any in showing the difference between an empty message
> and there being no exception at all.
>
> > It seems odd to return a sentry value
> > instead of a simple empty literal "", no other phobos string function
> > that I know of uses this technique.
>
> They also almost never care about null vs empty.
>
> Maybe it _would_ be better if it returned "" rather than emptyExceptionMsg,
> but it has to avoid returning null when there's actually an exception, and
> having emptyExceptionMsg both avoids any issues with null == "" and makes it
> much clearer when you print the string.
>
> You can certainly create a pull request for it if you want. I don't know
> what the other Phobos devs would think about it. Personally, I'm always a
> bit leery of relying on the difference between empty and null given the
> ridiculousness that is null == "".
Another thing to consider is that emptyExceptionMsg makes it easy to check for
"", whereas that's actually a bit of a pain if you're actually returning "".
assert(collectExceptionMsg(foo()) == emptyExceptionMsg);
clearly checks for "", whereas
assert(collectExceptionMsg(foo()) == "");
will be true if the result of collectExceptionMsg is null. You end up being
forced to save the result and do two checks:
immutable result = collectExceptionMsg(foo());
assert(result == "" && result !is null);
I don't know exactly what you're doing with collectExceptionMsg, but the
entire purpose that it was created for was unit testing that functions threw
exceptions of the correct type with the correct message, and emptyExceptionMsg
does a better job of that than returning "" would in the cases where you
actually want an exception's message to be null or "" (though that really
shouldn't be happening normally, since it's not exactly good practice).
Considering what it was designed for though, emptyExceptionMsg is better than
returning "" IMHO.
Regardless, if you think that you have a good enough use case and solid enough
argument for returning "", then by all means create a pull request for the
change.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list