If writef is fed an argument of an interface type, it just throws a FormatError.
----------
import std.stdio;
interface Qwert {}
class Yuiop : Qwert {
char[] toString() {
return "asdfg";
}
}
void main() {
Qwert hjkl = new Yuiop;
writefln(hjkl);
writefln("%s", hjkl);
}
----------
Error: std.format formatArg
----------
It should use the object's toString property. True, interfaces don't extend Object and so don't have toString as such, but what could be more sensible than casting it to an Object and then treating it in the same way?
Adding char[] toString() to the interface makes no difference.
Comment #1 by wbaxter — 2007-12-05T23:41:05Z
I'd say this is an outright bug. It can't cast to Object because it could be a COM interface, but if the interface explicitly has a toString method, then writef should definitely call it rather than throwing an exception.
Comment #2 by smjg — 2007-12-06T13:04:42Z
Surely it's not being a COM _interface_, but being a COM _object_ external to D that would be a problem, right?
I suppose the question is whether the runtime can determine if it's a D object. If so, the TypeInfo for interfaces could define this behaviour: If the interface defines a suitable toString, use it. Otherwise, see at runtime whether the interface instance is a D object. If so, convert it to an Object and use its toString, otherwise throw an exception.
If this runtime checking isn't possible (or isn't worth the implementation cost), then having a documented requirement that the interface defines a toString for it to work is a possibility, but it still has its drawbacks....
Comment #3 by dsimcha — 2009-03-27T19:43:52Z
*** Bug 2200 has been marked as a duplicate of this bug. ***