Bug 3715 – std.string.format can't use const/immutable toString functions
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-01-17T09:11:00Z
Last change time
2015-06-09T01:27:03Z
Assigned to
nobody
Creator
tomeksowi
Comments
Comment #0 by tomeksowi — 2010-01-17T09:11:29Z
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 by issues.dlang — 2010-09-17T23:48:07Z
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 by schveiguy — 2010-09-18T10:33:52Z
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 by tomeksowi — 2010-09-19T06:26:24Z
(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 by schveiguy — 2010-09-20T04:36:35Z
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 by dfj1esp02 — 2010-12-02T22:11:40Z
> 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 by andrej.mitrovich — 2012-12-21T09:09:38Z