www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - toString why not const

reply Tom <tom nospam.com> writes:
Hi, I'm trying to override Object's toString. I've noted it isn't a 
const method, namely:

string toString() const;

This cause me troubles when using it on a const reference.

Shouldn't it be const?

Thanks,
Tom;
Jan 21 2011
next sibling parent reply Jesse Phillips <jessekphillips+D gmail.com> writes:
Tom Wrote:

 Hi, I'm trying to override Object's toString. I've noted it isn't a 
 const method, namely:
 
 string toString() const;
 
 This cause me troubles when using it on a const reference.
 
 Shouldn't it be const?
 
 Thanks,
 Tom;
Phobos hasn't become very const aware. There have been bugs and things preventing such an effort. So I do believe it should be const, but between the other priorities, and possible signature change for toString, it isn't done yet.
Jan 21 2011
parent bearophile <bearophileHUGS lycos.com> writes:
Jesse Phillips:

 So I do believe it should be const, but between the other priorities, and
possible signature change for toString, it isn't done yet.
There also the idea of introducing the writeTo() standard method, that's neat :-) Bye, bearophile
Jan 21 2011
prev sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Friday, January 21, 2011 13:02:56 Tom wrote:
 Hi, I'm trying to override Object's toString. I've noted it isn't a
 const method, namely:
 
 string toString() const;
 
 This cause me troubles when using it on a const reference.
 
 Shouldn't it be const?
 
 Thanks,
 Tom;
It's a long-standing bug: http://d.puremagic.com/issues/show_bug.cgi?id=1824 In theory, the issues with const in the compiler and in Phobos will be focused on and sorted out not too long after the 64-bit port of dmd has been finished, but regardless, it's still an outstanding issue and definitely annoying. Also, be warned that (due to another bug) you _can't_ declare a struct's toString to be const (it must be _exactly_ string toString() { ... }). You _can_, however, override toString() to be const on classes, though how much good it really does you, I'm not sure. Hopefully all that stuff will be sorted out semi-soon. - Jonathan M Davis
Jan 21 2011