toString or not toString

Jonathan M Davis jmdavisProg at gmx.com
Tue Aug 30 19:41:37 PDT 2011


On Tuesday, August 30, 2011 20:59:06 Paul D. Anderson wrote:
> Can someone clarify for me the status and/or direction of string formatting
> in D?
> 
> We've got:
> 
> 1. toString, the object method with no parameters.
> 2. toString(sink, format)
> 3. to!String()
> 4. format
> 5. writef/writefln
> 6. write/writeln
> 
> I realize these exist for various reasons, some (1,3) are simple
> (unformatted) conversions, others (2,4-6) are designed to provide
> configurable formatting. The problem is that they are inconsistent with
> each other.
> 
> Using std.bigint as an example: 1, 3, 4 and 6 don't work, or don't work as
> expected (to me at least). 1. prints 'BigInt', 3 and 4 are compile errors.
> 
> I know bigint is a controversial example because Don has strong feelings
> against 1 and favors 2. (See bug #5231). I don't really have an opinion one
> way or the other but I need to know what to implement in my
> arbitrary-precision floating point module. This obviously relies heavily on
> bigint.
> 
> So, is there a transition underway in the language (or just Phobos) from
> toString, writeln and format, to toString(sink,format) and writefln?
> 
> Or is this just a divergence of views, both of which are acceptable and
> we'll have to get used to choosing one or the other?
> 
> Or am I just mistaken in believing there is any significant conflict?
> 
> I apologize if this has already been hashed out in the past and, if so, I
> would appreciate someone pointing me to that discussion. (Or just the
> results of the discussion.)

At this point, it's toString with no parameters. Don's completely out in left 
field with regards to how things currently work. I believe that BigInt is the 
_only_ example of toString(sink, format).

to!string is what you use when converting generic stuff to a string, and is 
probably better to use than calling toString directly. format is used when 
formatting strings separate for printing. write and writeln are for printing 
strings, and writef and writefln are for printing strings using formatting. I 
don't understand why there would be any confusion over the printing functions. 
If you want an automatic newline, then you pick one that ends in ln, and if 
you want formatting, then you pick one that ends in f (fln for both). The 
printing functions are not going to change at this point, and neither is 
format. They're for different purposes.

Now, what may change is toString on objects. In part due to Don's stance on 
the matter, there has been some discussion of creating a new function to 
replace toString called writeTo, which would be similar to toString(sink, 
format). It would integrate with std.conv.to, format, and the printing 
functions. And if you wanted to convert something to a string, you'd use 
to!string rather than calling writeTo directly. The DIP for it is here:

http://www.prowiki.org/wiki4d/wiki.cgi?LanguageDevel/DIPs/DIP9

Unfortunately however, the proposal seems to have gone nowhere thus far. Until 
it does, pretty much every object is just going to use toString without 
parameters, and the problems with BigInt's toString remain. However, if the 
proposal actually gets implemented, then the issue should then be able to be 
sorted out. Objects would have writeTo and toString would presumably be 
deprecated.

- Jonathan M Davis


More information about the Digitalmars-d mailing list