About to!int

bearophile bearophileHUGS at lycos.com
Tue Jan 31 05:05:37 PST 2012


Jonathan M Davis:

> and it's the sort of thing that 
> he's likely to consider bikeshedding at this point -

He isn't the only Phobos developer. It's a change able to improve my usage of conversions, so it's not a neutral change (as I think 'bikeshedding' implies).


> particularly since he's 
> increasingly against making small changes like this.

This is quite bad, because many things in Phobos were designed in a very quick way, with essentially no usage feedback from a larger community of users, so several details are sub-optimal. The only way to not carry on for a lot of time such early mistakes is to accept to fix them.


> Also, the argument can easily be made that " 10 " is _not_ a number. It's a 
> number surrounded by spaces. In general, std.conv.to does exact conversions, 
> and to!int(" 10 ") is _not_ an exact conversion, since it includes characters 
> which are not digits or a leading minus sign.

I see.
Walter doesn't like little functions in Phobos, so you can't just add a little function to Phobos that contains a strip followed a to!T.


> std.conv.parse, on the other 
> hand, deals with the spaces, because it's parsing the string rather than 
> converting the whole thing.

I have just answered to Zardoz why parse() is very bad for this purpose.


> Also, if you _want_ to do an exact conversion, and std.conv.to removed the 
> whitespace for you, then you couldn't use std.conv.to. You'd have to create 
> your own.

I see.


> On the other hand, if you want to ignore whitespace in the 
> conversion, you can easily wrap std.conv.to to do it.

Right. I will add a small function to my dlibs2 then. A small problem of doing it, is that my future snippets (even ones shown on newsgroups) tend to contain functions that aren't present in Phobos.


> So, it's more composable as it is.

This is true and it's an interesting point. From my experience composability is useful because it gives you more flexibility and offers you ways to do everything you want to do, but having "good defaults" (with functions that pack different functionality that is commonly needed together) leads to easy to write code. So when you design library functions you need to find a tradeoff between the two opposed desires.

In Haskell (both its Prelude and in most of the libraries I've seen) they solve this problem giving both the elementary function and functions composed by only two or three functions, that Walter doesn't like such shallow functions in Phobos :-)


> I don't see std.conv.to changing.

We'll see.

Thank you for your good answers,
bye,
bearophile


More information about the Digitalmars-d-learn mailing list