February 23, 2004 Re: Object's toString | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sigbjørn Lund Olsen | On Mon, 23 Feb 2004 16:58:03 +0100 (02/24/04 02:58:03) , Sigbjørn Lund Olsen <sigbjorn@lundolsen.net> wrote: > Y.Tomino wrote: > >> I think it's unnecessary that Object class has toString. >> Maybe, we use flat toString of std.math2, std.date, std.string or etc more >> than Object.toString, >> and Object.toString was small hindrance on writing my class's method. >> Also, most of classes don't need toString. >> >> It's probably smart that move toString from Object class to new interface. >> >> interface Showable >> { >> char[] toString(); >> } > > I feel I must agree here, though to nitpick I believe 'Serializable' would be a more appropriate interface name. Not all classes are serializable. If you have a File class, you can return the contents as a string - very useful, because you now have a standard way to serialize a serializable object, which can be used in functions/methods that process strings, like for example CRC32(File.toString). But what about if you have a RNG class (random number generator)? How would you serialize that? Or what if you're a programmer that swears to OO like it was the best thing since sliced bread, and therefore implemented pretty much everything as an object... How then would you serialize the Parser object of a symbolic calculator? > > Being capable of representing the object as a string only seems useful in some cases, and in others you'd generally just forget about implementing it. So why force a method onto every object, that only a subset of objects will need? > > Is there actually a reason behind having toString in Object and not a separate interface? If so I'd like to hear it... I'm admittedly not a very experienced programmer (Walter was writing C++ compilers around the time I was born), so maybe I've missed out on some vital piece here, but as it is this implementation of toString() seems very illogical to me. It even smells of a foul bletcherous Javaism that needs to be sent into early retirement. I understand serialization to be a way of creating a snapshot of an object's 'state', commonly done in a human readable format. In theory, one could recreate the state of an object from the serialized data. This covers simple case of converting some objects to a string form based on their current data value(s). It also could apply to a RNG, to record the current seed, and other values used in generating the next random number. It could apply to a Parser object to record the internal state of the parser at a specific moment in time. As such, it could be used as a debugging tool as well (along with other things). -- Derek -- Derek |
Copyright © 1999-2021 by the D Language Foundation