Issue 3715 - std.string.format can't use const/immutable toString functions
Summary: std.string.format can't use const/immutable toString functions
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-01-17 09:11 UTC by Tomasz Sowiński
Modified: 2015-06-09 01:27 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Tomasz Sowiński 2010-01-17 09:11:29 UTC
Test case:

struct A {
    string toString() const {
        return "A";
    }
}

void main() {
    writeln(format("%s", A()));
}

Outputs:
std.format.FormatError: std.format Can't convert test.A to string: "string toStr
ing()" not defined

Same for immutable.
Comment 1 Jonathan M Davis 2010-09-17 23:48:07 UTC
nothrow and pure don't work either. My guess is that toString() must have the _exact_ signature of public string toString() or it doesn't work with format(), so _no_ other function modifiers can be used on it.
Comment 2 Steven Schveighoffer 2010-09-18 10:33:52 UTC
Yes, std.string.format uses varargs, not templates, so it can only do what the TypeInfo allows it to do.  And the compiler will only store the function pointer to toString if it exactly matches the right signature.

See some related discussion in http://d.puremagic.com/issues/show_bug.cgi?id=4290
Comment 3 Tomasz Sowiński 2010-09-19 06:26:24 UTC
(In reply to comment #2)
> Yes, std.string.format uses varargs, not templates, so it can only do what the
> TypeInfo allows it to do.  And the compiler will only store the function
> pointer to toString if it exactly matches the right signature.

OK, now I understand why it doesn't work, thanks. But how to get it working? In real life toString() will often be const|pure|nothrow because of its nature.
Comment 4 Steven Schveighoffer 2010-09-20 04:36:35 UTC
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.
Comment 5 anonymous4 2010-12-02 22:11:40 UTC
> Right now, there are no implicit casts between function
> pointers (except const casts which are invalid anyways).

Looks like this is "fixed", see bug 3797.
Comment 6 Andrej Mitrovic 2012-12-21 09:09:38 UTC
Fixed in 2.060.