try/catch idiom in std.datetime
Jonathan M Davis
jmdavisProg at gmx.com
Wed Nov 20 23:44:29 PST 2013
On Thursday, November 21, 2013 08:20:39 Jesse Phillips wrote:
> On Monday, 18 November 2013 at 22:17:16 UTC, Jonathan M Davis
>
> wrote:
> > You validate by parsing the
> > string, and you extract the necessary data from it by parsing
> > it.
>
> I feel like you and Walter are both saying the same thing.
>
> fromISOString sounds to me like a data validation function. It is
> one that once validated provides a "validated" type, much like
> SortedRange.
>
> I don't think Walter disagrees with that, but isn't calling it
> out specifically. Did I get that right?
As I understand it, when Walter is talking about a validator function, _all_
it does is validate the input. It doesn't operate on the data beyond doing
validation - the idea being that you validate that the input has some
particular property or properties and then all the code that follows can just
assume that rather than checking it again. For instance, you could have a
function which validated that a string was valid UTF-8 and then have all code
that follows just assume that it was valid UTF-8 without ever checking. The
validator function is a separate function from any of the functions that
actually do any of the work.
So, to do that with fromISOExtString, you'd have something like
enforce(isValidISOExtString(str)); //validator function
auto data = Date.fromISOExtString(str); //func which actually does the work
and fromISOExtString wouldn't do any validation at all. It would assume that
the caller had already done the validation - either by calling the validator
function - isValidISOExtString - or by knowing that it was valid based on
where it came from (e.g. from toISOExtString).
In many cases, this makes sense from the standpoint of efficiency, because it
allows you to check once and then avoid checking in the rest of the code
rather than checking at various points throughout the code and potentially
duplicating the checks that way. However, in the case of fromISOExtString (as
well as many other parsing functions), you are essentially required to parse
the string in order to validate it, meaning that if you had
isValidISOExtString, and you called it before fromISOExtString, you'd be
doubling the amount of work. fromISOExtString might be able to avoid a few of
its checks if it knew that it could assume that the string was already valid,
but a large portion of the work would have to be done regardless simply
because that's what it takes to parse the date from the string. As such,
having a separate validator function doesn't make a lot of sense. Rather, it
makes more sense to have the function validate its own input rather than
assume that the caller already validated it. But Walter seems to be arguing in
favor of having a separate validator function all the time (or very close to
all the time) - including in this particular case. So, I don't think that
we're saying the same thing at all.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list