digitalmars.D.learn - Why does toString() exist? It seems useless.
- Don (14/14) Aug 18 2008 I cannot understand the rationale for a toString() member function which...
- Lars Ivar Igesund (10/28) Aug 18 2008 It is not useless, but it would indeed be useful to be able to pass a fo...
- bearophile (6/8) Aug 18 2008 I think the rationale is that it's a simple function that performs a sim...
- Don (30/40) Aug 18 2008 I think it encourages a very bad programming style. It makes ONE case
- Frank Benoit (23/25) Aug 18 2008 What do you think about doing one of
- bearophile (5/8) Aug 18 2008 You may be right, I am learning still a lot about such matters.
- Max Samukha (27/67) Aug 20 2008 FWIW, .NET provides IFormattable interface that allows objects to
- Steven Schveighoffer (22/36) Aug 18 2008 As you have discovered, it is for simple debugging.
- Fawzi Mohamed (23/52) Aug 20 2008 yes
- Benji Smith (17/19) Aug 18 2008 I agree.
- Robert Fraser (4/21) Aug 19 2008 I think both Tango and phobos allow objects to be converted to strings
- Don (19/41) Aug 20 2008 And that's the problem -- it's a really poor design, totally unsuitable
- Sean Kelly (7/56) Aug 20 2008 We had actually asked for some compiler changes to support this sort of
- Lars Ivar Igesund (9/65) Aug 20 2008 It was more of a suggestion that something accommodating this should exi...
- Sean Kelly (3/61) Aug 21 2008 Oops, I stand corrected.
- BCS (4/24) Aug 18 2008 I think the point is to have a least common denominator "Just give me *s...
- Christopher Wright (11/13) Aug 18 2008 Nothing similar exists in Tango because, for structs, it requires
- JAnderson (7/25) Aug 20 2008 I think you can do much the same as iostream and still take advantage of...
- Leandro Lucarella (11/14) Aug 22 2008 I think C++ formatting is heavily ill. All the manipulator stuff is
- Sean Kelly (13/23) Aug 22 2008 It's horrific but surprisingly flexible. I've created stateful
- Leandro Lucarella (13/36) Aug 22 2008 I agree, I meant that D should do better. Like templates are for gurus i...
- Fawzi Mohamed (7/22) Aug 22 2008 I think that putting formatting details in the stream is a very good
- Sean Kelly (12/37) Aug 22 2008 Yeah, the above example was a simplified version of some pretty fancy
- Fawzi Mohamed (10/46) Aug 23 2008 Yes I think that it is the correct approach to have special streams for
- Christopher Wright (7/15) Aug 22 2008 I strongly favor library solutions over language solutions for this.
- Don (3/14) Aug 24 2008 I agree. Which means that having I/O functions call toString() is a
- Ary Borenszweig (4/13) Aug 24 2008 But... writefln is used a lot for debugging purposes. If you quickly
I cannot understand the rationale for a toString() member function which doesn't support formatting. I don't think I've ever made a class which only has a single format option. If the class contains any integers, I want to specify whether it should use hex or decimal, leading zeros, sign. If it contains floating point numbers, it's number of digits, scientific notation, hex or decimal, and what to do with NaNs. And of course there are locales. Seriously, I cannot remember ever not having this requirement, so I cannot imagine a use for toString(). C++ got around this by giving state to the iostream classes. As far as I can tell, neither Phobos nor Tango provide any support at all. Practical example: BigInt (which I'm developing in both Tango and Phobos). It is a struct. How can it support I/O? I can't work out how to do it in either library.
Aug 18 2008
Don wrote:I cannot understand the rationale for a toString() member function which doesn't support formatting. I don't think I've ever made a class which only has a single format option. If the class contains any integers, I want to specify whether it should use hex or decimal, leading zeros, sign. If it contains floating point numbers, it's number of digits, scientific notation, hex or decimal, and what to do with NaNs. And of course there are locales. Seriously, I cannot remember ever not having this requirement, so I cannot imagine a use for toString(). C++ got around this by giving state to the iostream classes. As far as I can tell, neither Phobos nor Tango provide any support at all. Practical example: BigInt (which I'm developing in both Tango and Phobos). It is a struct. How can it support I/O? I can't work out how to do it in either library.It is not useless, but it would indeed be useful to be able to pass a format string to it - toString(char[] fmt) - or similar. Note that if structs could have interfaces, then this wouldn't really be a problem. -- Lars Ivar Igesund blog at http://larsivi.net DSource, #d.tango & #D: larsivi Dancing the Tango
Aug 18 2008
Don Wrote:I cannot understand the rationale for a toString() member function which doesn't support formatting.I think the rationale is that it's a simple function that performs a simple thing with an easy to use and easy to learn syntax, I have found it useful often. For more complex purposes you can write your own printing methods, or you can write a method that changes the printing state of the class/struct, that is later read and used when you call toString() and used to change its behavior. So I have created a graph class that by default when you call toString() gives a compact and simple textual representation of the graph. Then there are other methods, one that returns a string that shows the matrix of links, another that shows the weights of the arcs too, etc. There is also the possibility to modify the printing defaults with another method, to then later toString() acts differently. Bye, bearophile
Aug 18 2008
bearophile wrote:Don Wrote:I think it encourages a very bad programming style. It makes ONE case very easy, but does nothing for the other cases. It doesn't scale at all.I cannot understand the rationale for a toString() member function which doesn't support formatting.I think the rationale is that it's a simple function that performs a simple thing with an easy to use and easy to learn syntax, I have found it useful often.For more complex purposes you can write your own printing methods,or you can write a method that changes the printing state of the class/struct, that is later read and used when you call toString() and used tochange its behavior. But the way it is formatted should be a property of the output stream, I think, not a property of the object. Certainly this is the way C and C++ I/O works.So I have created a graph class that by default when you call toString() gives a compact and simple textual representation of the graph.Then there are other methods, one that returns a string that shows the matrix of links, another that shows the weights of the arcs too, etc. There is also the possibility to modify the printing defaults with another method, to then later toString() acts differently. Then I got suspicious that this might have come from Java or .NET. I googled for toString in Java and found: --- http://www.javapractices.com/topic/TopicAction.do?Id=55 --- "The toString method is widely implemented. It provides a simple, convenient mechanism for debugging classes during development. It is also widely used for logging, and for passing informative error messages to Exception constructors and assertions. When used in these informal ways, the exact format of toString is not part of the contract of the method, and callers should not rely on the exact format of the returned String." --- Now this makes sense. It's a useful hack. If it's just for debugging and error messages, that's fine. But if that is what it's for, the normal runtime I/O should discourage its use elsewhere (eg, by appending the class name in front). OTOH if it is intended for general I/O, I think it's a horribly broken design.
Aug 18 2008
Don schrieb:OTOH if it is intended for general I/O, I think it's a horribly broken design.What do you think about doing one of 1.) change signature in object.Object to char[] toString( char[] fmt = null) to give the object the possibility to use the format information. Well, would break all code :/ 2.) Create a standard interface for doing that interface Formatable { char[] toString( char[] fmt ); } The object can implement this interface, the user can try to cast to this interface. if( auto formatable = cast(Formatable)obj ){ print( formatable.toString(fmt)); } else if( obj !is null ){ print( obj.toString()); } else{ print( "null" ); }
Aug 18 2008
Don:But the way it is formatted should be a property of the output stream, I think, not a property of the object. Certainly this is the way C and C++ I/O works.You may be right, I am learning still a lot about such matters. But how can the output stream know about the many ways to print a graph? (I don't know enough about C++ streams). Bye, bearophile
Aug 18 2008
On Mon, 18 Aug 2008 15:15:51 +0200, Don <nospam nospam.com.au> wrote:bearophile wrote:FWIW, .NET provides IFormattable interface that allows objects to imlement custom formatting: interface IFormattable { string toString(string fmt, IFormatProvider fProvider); } class C : IFormattable { string toString(string fmt, IFormatProvider fProvider) { if (fProvider !is null) { // try to get a formater for this type // from fProvider } else { switch (fmt) { // do formatting based on the format string } } } }Don Wrote:I think it encourages a very bad programming style. It makes ONE case very easy, but does nothing for the other cases. It doesn't scale at all.I cannot understand the rationale for a toString() member function which doesn't support formatting.I think the rationale is that it's a simple function that performs a simple thing with an easy to use and easy to learn syntax, I have found it useful often.For more complex purposes you can write your own printing methods,or you can write a method that changes the printing state of the class/struct, that is later read and used when you call toString() and used tochange its behavior. But the way it is formatted should be a property of the output stream, I think, not a property of the object. Certainly this is the way C and C++ I/O works.So I have created a graph class that by default when you call toString() gives a compact and simple textual representation of the graph.Then there are other methods, one that returns a string that shows the matrix of links, another that shows the weights of the arcs too, etc. There is also the possibility to modify the printing defaults with another method, to then later toString() acts differently. Then I got suspicious that this might have come from Java or .NET.I googled for toString in Java and found: --- http://www.javapractices.com/topic/TopicAction.do?Id=55 --- "The toString method is widely implemented. It provides a simple, convenient mechanism for debugging classes during development. It is also widely used for logging, and for passing informative error messages to Exception constructors and assertions. When used in these informal ways, the exact format of toString is not part of the contract of the method, and callers should not rely on the exact format of the returned String." --- Now this makes sense. It's a useful hack. If it's just for debugging and error messages, that's fine. But if that is what it's for, the normal runtime I/O should discourage its use elsewhere (eg, by appending the class name in front). OTOH if it is intended for general I/O, I think it's a horribly broken design.
Aug 20 2008
"Don" wroteI cannot understand the rationale for a toString() member function which doesn't support formatting. I don't think I've ever made a class which only has a single format option. If the class contains any integers, I want to specify whether it should use hex or decimal, leading zeros, sign. If it contains floating point numbers, it's number of digits, scientific notation, hex or decimal, and what to do with NaNs. And of course there are locales. Seriously, I cannot remember ever not having this requirement, so I cannot imagine a use for toString(). C++ got around this by giving state to the iostream classes. As far as I can tell, neither Phobos nor Tango provide any support at all. Practical example: BigInt (which I'm developing in both Tango and Phobos). It is a struct. How can it support I/O? I can't work out how to do it in either library.As you have discovered, it is for simple debugging. However, it's not even close to as useful as Java, where if you combine a string with an object, it automatically calls the toString function. As far as supporting an output method, I think toString is completely the wrong way to look at it. A simple example is a container class, such as a vector of ints. Let's say you are outputting a vector of ints every time it has changed. What ends up happening is you build a (possibly huge) string every time you output the vector, and then are throwing the string away! When all you really need is about 20 bytes of stack space to create the individual int strings and then output to an i/o function. Even with a format string, that doesn't solve this problem. What I would like to see is an output method in Object that looks like: int output(int delegate(char[] x) sink, char[] fmt = null); And something like how 'toString' works in a struct (where there is an xoutput function in the struct typeinfo class). This is a generic solution, it would be customized to the appropriate i/o library of Tango or Phobos. Possibly the format strings would be different based on the I/O (i.e. Phobos uses printf style formatting constructs, Tango uses .NET style). -Steve
Aug 18 2008
On 2008-08-18 16:28:36 +0200, "Steven Schveighoffer" <schveiguy yahoo.com> said:"Don" wroteyesI cannot understand the rationale for a toString() member function which doesn't support formatting. [...]As you have discovered, it is for simple debugging.However, it's not even close to as useful as Java, where if you combine a string with an object, it automatically calls the toString function. As far as supporting an output method, I think toString is completely the wrong way to look at it. A simple example is a container class, such as a vector of ints. Let's say you are outputting a vector of ints every time it has changed. What ends up happening is you build a (possibly huge) string every time you output the vector, and then are throwing the string away! When all you really need is about 20 bytes of stack space to create the individual int strings and then output to an i/o function. Even with a format string, that doesn't solve this problem. What I would like to see is an output method in Object that looks like: int output(int delegate(char[] x) sink, char[] fmt = null);I fully agree I normally write a method that writes to a stream, with handles to tailor the output. I also often write a desc method that gives a detailed description of all the variables of the object. Then I write toString that calls one of these methods with some default arguments on a special stream the creates a string. In general stream based output is much superior to conversion to a string (more efficient and often simpler to write), and when needed one can use a special stream that returns a string. In tango I use a Print!(T) instance as nice to use stream, nicer than a sink delegate. I wrote a Stringify class that like Python StringIO returns the collected string upon request.And something like how 'toString' works in a struct (where there is an xoutput function in the struct typeinfo class).yes would also be nice to haveThis is a generic solution, it would be customized to the appropriate i/o library of Tango or Phobos. Possibly the format strings would be different based on the I/O (i.e. Phobos uses printf style formatting constructs, Tango uses .NET style).I am not too sure that I want to always have a format string, encoding and decoding it might be tedious, normally having a method with extra arguments is better. Serialization is another topic, there the format should be defined by the stream. Fawzi
Aug 20 2008
Don wrote:I cannot understand the rationale for a toString() member function which doesn't support formatting.I agree. In Java, the toString() method *must* exist on the base Object class, because of constructs like this: String s = "hello" + (new World()); Without an implementation of toString(), it'd be impossible to support those kinds of automatic String conversion and concatenation (which actually do end up being pretty handy in logging & debugging statements). But since D doesn't support implicit String conversion, it seems pretty pointless. Incidentally, I'd be opposed to a toString(char[] format) method being a part of the Object class. I prefer the Tango (.NET style) formatting, and others surely prefer the printf style formatting in Phobos. If formatting strings became a part of the basic Object definition, I can imagine a lot of very annoyed developers, depending on which formatting style was chosen for Object. --benji
Aug 18 2008
Benji Smith wrote:Don wrote:I think both Tango and phobos allow objects to be converted to strings when specified as parameters to a formatting function -- i.e. Stdout.format("{0}", obj) will call obj.toString().I cannot understand the rationale for a toString() member function which doesn't support formatting.I agree. In Java, the toString() method *must* exist on the base Object class, because of constructs like this: String s = "hello" + (new World()); Without an implementation of toString(), it'd be impossible to support those kinds of automatic String conversion and concatenation (which actually do end up being pretty handy in logging & debugging statements). But since D doesn't support implicit String conversion, it seems pretty pointless.
Aug 19 2008
Robert Fraser wrote:Benji Smith wrote:And that's the problem -- it's a really poor design, totally unsuitable for that purpose. Simple challenge: class Foo { public: double x; } 1. You want it to output x in the default way for the current locale. (eg, 5,35 or 5.35 depending on which country you're in). Write toString(). 2. Now write toString() such that: double y = 3.156892123; Foo z = new Foo; z.x = 3.156892123; writefln("%.3f, %.3f", y, z); writefln("%.4f, %.4f", y, z); (or Tango equivalent) produces the same results for y and z. Both of these should be trivial.Don wrote:I think both Tango and phobos allow objects to be converted to strings when specified as parameters to a formatting function -- i.e. Stdout.format("{0}", obj) will call obj.toString().I cannot understand the rationale for a toString() member function which doesn't support formatting.I agree. In Java, the toString() method *must* exist on the base Object class, because of constructs like this: String s = "hello" + (new World()); Without an implementation of toString(), it'd be impossible to support those kinds of automatic String conversion and concatenation (which actually do end up being pretty handy in logging & debugging statements). But since D doesn't support implicit String conversion, it seems pretty pointless.
Aug 20 2008
Don wrote:Robert Fraser wrote:We had actually asked for some compiler changes to support this sort of formatting (necessary since the function signature for toString is hardcoded in the compiler for structs), but the request was denied, presumably as unnecessary. I don't remember the details of the request however--Lars would have to provide that. SeanBenji Smith wrote:And that's the problem -- it's a really poor design, totally unsuitable for that purpose. Simple challenge: class Foo { public: double x; } 1. You want it to output x in the default way for the current locale. (eg, 5,35 or 5.35 depending on which country you're in). Write toString(). 2. Now write toString() such that: double y = 3.156892123; Foo z = new Foo; z.x = 3.156892123; writefln("%.3f, %.3f", y, z); writefln("%.4f, %.4f", y, z); (or Tango equivalent) produces the same results for y and z. Both of these should be trivial.Don wrote:I think both Tango and phobos allow objects to be converted to strings when specified as parameters to a formatting function -- i.e. Stdout.format("{0}", obj) will call obj.toString().I cannot understand the rationale for a toString() member function which doesn't support formatting.I agree. In Java, the toString() method *must* exist on the base Object class, because of constructs like this: String s = "hello" + (new World()); Without an implementation of toString(), it'd be impossible to support those kinds of automatic String conversion and concatenation (which actually do end up being pretty handy in logging & debugging statements). But since D doesn't support implicit String conversion, it seems pretty pointless.
Aug 20 2008
Sean Kelly wrote:Don wrote:It was more of a suggestion that something accommodating this should exist, with toString(char[] fmt) as an initial suggestion. I think it just slipped away as it wasn't a good enough solution, not really denied. -- Lars Ivar Igesund blog at http://larsivi.net DSource, #d.tango & #D: larsivi Dancing the TangoRobert Fraser wrote:We had actually asked for some compiler changes to support this sort of formatting (necessary since the function signature for toString is hardcoded in the compiler for structs), but the request was denied, presumably as unnecessary. I don't remember the details of the request however--Lars would have to provide that.Benji Smith wrote:And that's the problem -- it's a really poor design, totally unsuitable for that purpose. Simple challenge: class Foo { public: double x; } 1. You want it to output x in the default way for the current locale. (eg, 5,35 or 5.35 depending on which country you're in). Write toString(). 2. Now write toString() such that: double y = 3.156892123; Foo z = new Foo; z.x = 3.156892123; writefln("%.3f, %.3f", y, z); writefln("%.4f, %.4f", y, z); (or Tango equivalent) produces the same results for y and z. Both of these should be trivial.Don wrote:I think both Tango and phobos allow objects to be converted to strings when specified as parameters to a formatting function -- i.e. Stdout.format("{0}", obj) will call obj.toString().I cannot understand the rationale for a toString() member function which doesn't support formatting.I agree. In Java, the toString() method *must* exist on the base Object class, because of constructs like this: String s = "hello" + (new World()); Without an implementation of toString(), it'd be impossible to support those kinds of automatic String conversion and concatenation (which actually do end up being pretty handy in logging & debugging statements). But since D doesn't support implicit String conversion, it seems pretty pointless.
Aug 20 2008
Lars Ivar Igesund wrote:Sean Kelly wrote:Oops, I stand corrected. SeanDon wrote:It was more of a suggestion that something accommodating this should exist, with toString(char[] fmt) as an initial suggestion. I think it just slipped away as it wasn't a good enough solution, not really denied.Robert Fraser wrote:We had actually asked for some compiler changes to support this sort of formatting (necessary since the function signature for toString is hardcoded in the compiler for structs), but the request was denied, presumably as unnecessary. I don't remember the details of the request however--Lars would have to provide that.Benji Smith wrote:And that's the problem -- it's a really poor design, totally unsuitable for that purpose. Simple challenge: class Foo { public: double x; } 1. You want it to output x in the default way for the current locale. (eg, 5,35 or 5.35 depending on which country you're in). Write toString(). 2. Now write toString() such that: double y = 3.156892123; Foo z = new Foo; z.x = 3.156892123; writefln("%.3f, %.3f", y, z); writefln("%.4f, %.4f", y, z); (or Tango equivalent) produces the same results for y and z. Both of these should be trivial.Don wrote:I think both Tango and phobos allow objects to be converted to strings when specified as parameters to a formatting function -- i.e. Stdout.format("{0}", obj) will call obj.toString().I cannot understand the rationale for a toString() member function which doesn't support formatting.I agree. In Java, the toString() method *must* exist on the base Object class, because of constructs like this: String s = "hello" + (new World()); Without an implementation of toString(), it'd be impossible to support those kinds of automatic String conversion and concatenation (which actually do end up being pretty handy in logging & debugging statements). But since D doesn't support implicit String conversion, it seems pretty pointless.
Aug 21 2008
Reply to don,I cannot understand the rationale for a toString() member function which doesn't support formatting. I don't think I've ever made a class which only has a single format option. If the class contains any integers, I want to specify whether it should use hex or decimal, leading zeros, sign. If it contains floating point numbers, it's number of digits, scientific notation, hex or decimal, and what to do with NaNs. And of course there are locales. Seriously, I cannot remember ever not having this requirement, so I cannot imagine a use for toString(). C++ got around this by giving state to the iostream classes. As far as I can tell, neither Phobos nor Tango provide any support at all. Practical example: BigInt (which I'm developing in both Tango and Phobos). It is a struct. How can it support I/O? I can't work out how to do it in either library.I think the point is to have a least common denominator "Just give me *something*" method for debugging and the like. Anything more demanding is to complicated for the base class as it will never be flexible enough.
Aug 18 2008
Don wrote:I cannot understand the rationale for a toString() member function which doesn't support formatting.Nothing similar exists in Tango because, for structs, it requires compiler support. In Tango, you can specify formatting for a particular type by subclassing Layout and overriding munge. Or rather, you could, except munge is private. Relevant ticket: http://dsource.org/projects/tango/ticket/1247 On the other hand, I've only ever used toString for debug output, using external formatters for anything with user-visible results. Again, if Layout.munge were not private, you could add external formatters easily. This helps you not at all when using Phobos.
Aug 18 2008
Don wrote:I cannot understand the rationale for a toString() member function which doesn't support formatting. I don't think I've ever made a class which only has a single format option. If the class contains any integers, I want to specify whether it should use hex or decimal, leading zeros, sign. If it contains floating point numbers, it's number of digits, scientific notation, hex or decimal, and what to do with NaNs. And of course there are locales. Seriously, I cannot remember ever not having this requirement, so I cannot imagine a use for toString(). C++ got around this by giving state to the iostream classes. As far as I can tell, neither Phobos nor Tango provide any support at all. Practical example: BigInt (which I'm developing in both Tango and Phobos). It is a struct. How can it support I/O? I can't work out how to do it in either library.I think you can do much the same as iostream and still take advantage of toString. Something like: string foo = a.toString() ~ format(a, "..."); Where format would be special overloaded template (with specialization) that does conversions. -Joel
Aug 20 2008
Don, el 18 de agosto a las 12:41 me escribiste:I cannot understand the rationale for a toString() member function which doesn't support formatting.I think is useful only for debugging purposes.C++ got around this by giving state to the iostream classes.I think C++ formatting is heavily ill. All the manipulator stuff is madness. -- Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/ ---------------------------------------------------------------------------- GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145 104C 949E BFB6 5F5A 8D05) ---------------------------------------------------------------------------- No existiría el sonido del mar si faltara en la vida oreja y caracol. -- Ricardo Vaporeso. Cosquín, 1908.
Aug 22 2008
Leandro Lucarella wrote:Don, el 18 de agosto a las 12:41 me escribiste:It's horrific but surprisingly flexible. I've created stateful formatters for various protocols and made it work all invisibly with the formatting hooks provided in C++. For example: std::cout << a << b << c << std::flush; The above may perform lazy output of structured XML, some binary encoding, etc. It might be possible to get close with toString, but because it returns an array rather than writing into a buffer some of the flexibiliy (like lazy encoding) would definitely be lost. My experience with Java suggests that toString is meant for debugging purposes anyway. It's rare that an object will produce meaningful user-level output with its toString method. SeanI cannot understand the rationale for a toString() member function which doesn't support formatting.I think is useful only for debugging purposes.C++ got around this by giving state to the iostream classes.I think C++ formatting is heavily ill. All the manipulator stuff is madness.
Aug 22 2008
Sean Kelly, el 22 de agosto a las 12:46 me escribiste:Leandro Lucarella wrote:I agree, I meant that D should do better. Like templates are for gurus in C++ and for people in D, formatting should be for people too in D (as opposed to C++ manipulators =). -- Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/ ---------------------------------------------------------------------------- GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145 104C 949E BFB6 5F5A 8D05) ---------------------------------------------------------------------------- He cometido pecados, he hecho el mal, he sido víctima de la envidia, el egoísmo, la ambición, la mentira y la frivolidad, pero siempre he sido un padre argentino que quiere que su hijo triunfe en la vida. -- Ricardo VaporesoDon, el 18 de agosto a las 12:41 me escribiste:It's horrific but surprisingly flexible. I've created stateful formatters for various protocols and made it work all invisibly with the formatting hooks provided in C++. For example: std::cout << a << b << c << std::flush; The above may perform lazy output of structured XML, some binary encoding, etc. It might be possible to get close with toString, but because it returns an array rather than writing into a buffer some of the flexibiliy (like lazy encoding) would definitely be lost. My experience with Java suggests that toString is meant for debugging purposes anyway. It's rare that an object will produce meaningful user-level output with its toString method.I cannot understand the rationale for a toString() member function which doesn't support formatting.I think is useful only for debugging purposes.C++ got around this by giving state to the iostream classes.I think C++ formatting is heavily ill. All the manipulator stuff is madness.
Aug 22 2008
On 2008-08-22 22:10:21 +0200, Leandro Lucarella <llucax gmail.com> said:Sean Kelly, el 22 de agosto a las 12:46 me escribiste:I think that putting formatting details in the stream is a very good idea for serialization, but not for normal textual output, the two things are not the same (even if it might be useful to use some xml serialization for some objects, and send it together with "normal" text). FawziLeandro Lucarella wrote:Don, el 18 de agosto a las 12:41 me escribiste:It's horrific but surprisingly flexible. I've created stateful formatters for various protocols and made it work all invisibly with the formatting hooks provided in C++. For example: std::cout << a << b << c << std::flush;I cannot understand the rationale for a toString() member function which doesn't support formatting.I think is useful only for debugging purposes.C++ got around this by giving state to the iostream classes.I think C++ formatting is heavily ill. All the manipulator stuff is madness.
Aug 22 2008
Fawzi Mohamed wrote:On 2008-08-22 22:10:21 +0200, Leandro Lucarella <llucax gmail.com> said:Yeah, the above example was a simplified version of some pretty fancy stuff I did for a multi-protocol server app. It made serialization painfully easy for the user (so long as the proper support code was built into the formatting frastructure), but was really operating well beyond the scope of what iostream formatting was intended to do. In general though I think it's preferable to have such things done differently than raw IO. Tango has some support for this with its Reader/Writer model in tango.io.protocol, but even that is somewhat limited to specific categories of protocols. For example, I recently tried using it for ASN.1 transcoding and it just isn't sufficient. SeanSean Kelly, el 22 de agosto a las 12:46 me escribiste:I think that putting formatting details in the stream is a very good idea for serialization, but not for normal textual output, the two things are not the same (even if it might be useful to use some xml serialization for some objects, and send it together with "normal" text).Leandro Lucarella wrote:Don, el 18 de agosto a las 12:41 me escribiste:It's horrific but surprisingly flexible. I've created stateful formatters for various protocols and made it work all invisibly with the formatting hooks provided in C++. For example: std::cout << a << b << c << std::flush;I cannot understand the rationale for a toString() member function which doesn't support formatting.I think is useful only for debugging purposes.C++ got around this by giving state to the iostream classes.I think C++ formatting is heavily ill. All the manipulator stuff is madness.
Aug 22 2008
On 2008-08-23 00:28:19 +0200, Sean Kelly <sean invisibleduck.org> said:Fawzi Mohamed wrote:Yes I think that it is the correct approach to have special streams for serialization, as long as they can then be hooked to the "normal" streams then everything is ok. It is a pity that Reader/Writer are not sufficient, could it be extended to support it, or a different approach is needed? Actually my needs are smaller, I would love to have at least a textual streaming protocol (xml based?) that works with Reader/Writer for quick dump/read of my objects.. FawziOn 2008-08-22 22:10:21 +0200, Leandro Lucarella <llucax gmail.com> said:Yeah, the above example was a simplified version of some pretty fancy stuff I did for a multi-protocol server app. It made serialization painfully easy for the user (so long as the proper support code was built into the formatting frastructure), but was really operating well beyond the scope of what iostream formatting was intended to do. In general though I think it's preferable to have such things done differently than raw IO. Tango has some support for this with its Reader/Writer model in tango.io.protocol, but even that is somewhat limited to specific categories of protocols. For example, I recently tried using it for ASN.1 transcoding and it just isn't sufficient.Sean Kelly, el 22 de agosto a las 12:46 me escribiste:I think that putting formatting details in the stream is a very good idea for serialization, but not for normal textual output, the two things are not the same (even if it might be useful to use some xml serialization for some objects, and send it together with "normal" text).Leandro Lucarella wrote:Don, el 18 de agosto a las 12:41 me escribiste:It's horrific but surprisingly flexible. I've created stateful formatters for various protocols and made it work all invisibly with the formatting hooks provided in C++. For example: std::cout << a << b << c << std::flush;I cannot understand the rationale for a toString() member function which doesn't support formatting.I think is useful only for debugging purposes.C++ got around this by giving state to the iostream classes.I think C++ formatting is heavily ill. All the manipulator stuff is madness.
Aug 23 2008
Leandro Lucarella wrote:Sean Kelly, el 22 de agosto a las 12:46 me escribiste:I strongly favor library solutions over language solutions for this. Even if there were a language solution, I would still want a library solution so I could better enforce separation of concerns. Unless I want to use dependency injection on my domain classes, which is an Abomination unto Nuggan, or put complex formatting logic into them, which is nearly as abhorrent.My experience with Java suggests that toString is meant for debugging purposes anyway. It's rare that an object will produce meaningful user-level output with its toString method.I agree, I meant that D should do better. Like templates are for gurus in C++ and for people in D, formatting should be for people too in D (as opposed to C++ manipulators =).
Aug 22 2008
Leandro Lucarella wrote:Don, el 18 de agosto a las 12:41 me escribiste:I agree. Which means that having I/O functions call toString() is a broken design (I'm looking at you, writefln!)I cannot understand the rationale for a toString() member function which doesn't support formatting.I think is useful only for debugging purposes.C++ got around this by giving state to the iostream classes.I think C++ formatting is heavily ill. All the manipulator stuff is madness.
Aug 24 2008
Don a écrit :Leandro Lucarella wrote:But... writefln is used a lot for debugging purposes. If you quickly want to print something to the console, it's much faster not having to use toString() everywhere.Don, el 18 de agosto a las 12:41 me escribiste:I agree. Which means that having I/O functions call toString() is a broken design (I'm looking at you, writefln!)I cannot understand the rationale for a toString() member function which doesn't support formatting.I think is useful only for debugging purposes.
Aug 24 2008