Current status of toString in phobos
Matthias Walter
xammy at xammy.homelinux.net
Thu Nov 18 14:52:19 PST 2010
On 11/18/2010 12:04 AM, Jonathan M Davis wrote:
> On Wednesday 17 November 2010 19:48:30 Matthias Walter wrote:
>
>> Hi,
>>
>> I'm currently using DMD v2.049 with phobos. I found an old discussion
>> about how toString should be designed and how it is supposed to work. As
>> the following code does not print out the number, I wonder what is the
>>
>> current status of how to implement a toString function for a struct/class:
>> | auto n = BigInt("42");
>> | writefln("%s", n);
>>
> Object has the function toString(), which you have to override.
>
> Structs have to define toString() as well. However, unlike classes, it's
> signature must be _exactly_ string toString(); You can't add extra modifiers
> such as const or nothrow, or it won't work. You _should_ be able to have extra
> modifiers on it, but it doesn't work at the moment if you do (so I typically end
> up declaring two toString()s - one with the modifiers and one without - and
> declare a private method which they both call that has the actual
> implementation). There's an open bug on it. Once it's fixed, any signature for
> toString() should work for structs as long as its name is toString() and it
> returns a string.
>
Okay, so the current way to use toString is still the Java-like way.
> As for BigInt, for some reason it doesn't have a normal toString(). Instead, it
> has one which you pass a delegate and format string to in order to control how
> it's converted to a string. It's probably useful, but I do think that it should
> have a normal toString() method as well. I've opened a bug report on it:
> http://d.puremagic.com/issues/show_bug.cgi?id=5231
>
I've read the bug comments. Seems as it will take some time until the
toString system gets renewed (although I like Don's way, but without the
corresponding format/writefln implementation, it is pretty useless).
Until then I will have a to use toDecimalString and toHex, I guess.
best regards
Matthias
More information about the Digitalmars-d-learn
mailing list