to!()() & leading/trailing whitespace

Norbert Nemec Norbert at Nemec-online.de
Tue Aug 17 23:49:28 PDT 2010


On 17/08/10 02:49, Andrei Alexandrescu wrote:
> bearophile wrote:
>> This post is about this enhancement request of mine that recently
>> David Simcha has closed as wontfix:
>> http://d.puremagic.com/issues/show_bug.cgi?id=4165
> [snip]
>> Phobos functions are meant as the most simpler bricks, that you may
>> compose to perform more complex operations: this is generally true
>> and good, but Python shows that when two or few operations are
>> frequently done attached to each other, it's good to put inside the
>> std lib something the performs the composed thing in one go, because
>> it helps chunk the code and makes the code shorter and more readable,
>> and decreases the chance for bugs. When I read numbers from files I
>> will need to use to!int(txt.strip()) often.
>
> I don't feel very strongly about this (in particular e.g. I do allow
> leading whitespace for floating-point parsing).
>
> My only problem is that sometimes people _don't_ want to ignore trailing
> whitespace, which becomes quite difficult. But then I guess that's a
> rare case.

Honestly: why should someone *want* the conversion to *fail* if there is 
additional whitespace? I would agree if to!()() would do something 
useful with the whitespace but as it is, the routine simply seems picky 
for the sake of being picky.

str<->number conversions are inherently inexact since there are various 
representations for a number. It is just the decision about how tolerant 
the routine should be. In my experience it makes sense to make the 
conversion as tolerant as possible as long as it remains predictable and 
there is no danger of misinterpreting input.


More information about the Digitalmars-d mailing list