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.
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.
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
(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.
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.
> 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.
Fixed in 2.060.