Comment #0 by john.loughran.colvin — 2016-03-01T16:17:12Z
struct S
{
Object ca;
void foo() const
{
import std.stdio;
writeln(this);
}
}
causes this error:
/Users/john/Git/phobos/std/format.d(2912): Error: template instance std.format.formatObject!(LockingTextWriter, const(Object), char) does not match template declaration formatObject(Writer, T, Char)(ref Writer w, ref T val, ref FormatSpec!Char f) if (hasToString!(T, Char))
/Users/john/Git/phobos/std/format.d(2645): Error: template instance std.format.formatValue!(LockingTextWriter, const(Object), char) error instantiating
/Users/john/Git/phobos/std/format.d(3164): instantiated from here: formatElement!(LockingTextWriter, const(Object), char)
/Users/john/Git/phobos/std/format.d(3477): instantiated from here: formatValue!(LockingTextWriter, const(S), char)
/Users/john/Git/phobos/std/format.d(467): instantiated from here: formatGeneric!(LockingTextWriter, const(S), char)
/Users/john/Git/phobos/std/stdio.d(1298): ... (1 instantiations, -v to show) ...
/Users/john/Git/phobos/std/stdio.d(3095): instantiated from here: write!(const(S), char)
test.d(8): instantiated from here: writeln!(const(S))
/Users/john/Git/phobos/std/format.d(470): Error: forward reference to inferred return type of function call (ref arg)
{
return cast(const(void*))&arg;
}
(_param_2)
/Users/john/Git/phobos/std/format.d(546): Error: template instance std.format.formatNth!(LockingTextWriter, char, const(S)) error instantiating
/Users/john/Git/phobos/std/stdio.d(1298): instantiated from here: formattedWrite!(LockingTextWriter, char, const(S))
/Users/john/Git/phobos/std/stdio.d(3095): instantiated from here: write!(const(S), char)
test.d(8): instantiated from here: writeln!(const(S))
Comment #1 by razvan.nitu1305 — 2017-01-25T16:11:49Z
The problem is that the function has the const identifier which implicitly makes this const, while writeln expects a non-const argument. I think that this isn't an issue, but a programming mistake; removing const from the functions will solve the issue.
Comment #2 by john.loughran.colvin — 2017-01-25T16:17:08Z
Simpler example with the same problem:
void main()
{
import std.stdio;
const Object o;
writeln(o);
}
Not using const is a workaround, but a) writeln should be able to work with a const Object and b) even if it can't, it shouldn't cause a forward reference error.
Comment #3 by Ajieskola — 2018-08-31T19:08:25Z
I just tested that with DMD v2.081.2, both of the mentioned examples now compile and run without errors. This issue is thus fixed.