[Issue 3715] std.string.format can't use const/immutable toString functions

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Sep 20 04:37:16 PDT 2010


http://d.puremagic.com/issues/show_bug.cgi?id=3715



--- Comment #4 from Steven Schveighoffer <schveiguy at yahoo.com> 2010-09-20 04:36:35 PDT ---
Well, first of all, toString should always be const.

But pure and nothrow are two options that are also valid.  I think some real
thought needs to go into delegates and function pointers, and what can
implicitly cast.  Right now, there are no implicit casts between function
pointers (except const casts which are invalid anyways).

As a workaround, you can do something like this:

struct A {
  /*pure nothrow?*/ string toString() const { return "A"; }
  string toString() {const(A)*me = &this; return me.toString();}
}

What this does is put a common implementation for each overload so you don't
have to rewrite it.

When the requirements change (and they will, because toString should be const),
then in most cases you can just get away with a const function.  If you want to
have pure and nothrow available, then you still have to do something like this.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list