On 9-mar-10, at 18:56, Steven Schveighoffer wrote:

On Tue, 09 Mar 2010 12:33:01 -0500, Andrei Alexandrescu <SeeWebsiteForEmail@erdani.org> wrote:

In wake of printing multi-dimensional arrays, I agree that start and end delimiters should be present by default. If delimiters are present, it only makes sense to make the array look like a D array, so the ", " becomes an acceptable proposition.

That's great!

I'm unsure about strings - should "to" go all gung-ho on quoting and escaping quotes etc.? That's a bit odd. Consider:

auto str = to!string("hello world");

I'd expect the call to be an identity application that makes str equal to "hello world". So far so good. Then say I convert with "to" an array of strings to a string:

auto str2 = to!string(["hello world"]);

Now str2 is "\"hello world\"", i.e. has an extra pair of quotes.

I think you mean "[\"hello world\"]"

well the thing is again str vs repr (string, or representation), repr should output something that is basically valid D code that reproduces the same value, whereas str not, what should to! do?

So "to" applied to an array does not always use "to" applied to each element of the array - it has a completely different behavior. I wonder whether that's a desirable behavior.

I would say no.  I guess you could make the argument that strings are already arrays treated specially, but I don't think it adds much to put the quotes there.  Plus, it allows simpler code and documentation, you can define the conversion of an array as a purely recursive function, even if it may not be implemented that way.

Note that ranges should convert identically to arrays, making arrays special would make things odd.

Another thing that's unclear to me is whether writeln and "to" should be defined such that

write(to!string(stuff))

and

writeln(stuff)

produce the same text. Currently that's the general plan, but I wonder whether we should change the approach.

then I find it much better to base everything on something like my 
void writeOut(T,S...)(void delegate char[]sink, T object,S formatting)
that outputs to a sink, so that you can have output without memory allocation.
(well my version is little more complex because I want to accept also other stuff not just a sink http://github.com/fawzi/blip/blob/master/blip/io/BasicIO.d )

If you want a single string you can easily write helpers like
T[] collectAppender(T)(void delegate(void delegate(T[])) appender,char[] buf=null)
or similar ( http://github.com/fawzi/blip/blob/master/blip/container/GrowableArray.d ) that convert sink based stuff to a single string.

I think that that design is very efficient and clean, well I have to admit that I *hate* toString and methods that convert stuff to string, because they are unnecessarily inefficent.

I like that, it shows consistency.  If you want to change the format, you can via to's parameters.  But the most useful defaults should be the same in writeln.

Even though to is a way to get a crude serialization function, I don't know if it will be sufficient or efficient for a serious serialization library (i.e. I don't think it's worth making 'to' that smart).  But it is sufficient as a debug tool, it just needs better defaults.

as I said serialization is a different beast (at least what I want from serialization), and the "how" should be in the target serializer, the serialization function should given enough information to the serializer, so that it might serialize how it pleases to him (in particular meaningful labels should be given for serialization to json,xml,...).

Fawzi