Bug 20632 – Format and writeln not usable with non-copyable types
Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2020-03-04T02:05:19Z
Last change time
2021-03-28T10:20:26Z
Keywords
rejects-valid
Assigned to
No Owner
Creator
Mathias LANG
Comments
Comment #0 by pro.mathias.lang — 2020-03-04T02:05:19Z
It is probably the evidence of a larger problem, and fixing it is probably limited by fixes to the ABI, but here it goes:
```
import std.stdio;
private struct MyStruct
{
int a;
string b;
@disable this(this);
@disable ref MyStruct opAssign(ref MyStruct other);
}
void main ()
{
MyStruct s;
writeln(s);
}
```
This code *should* compile. We should be able to take a value by ref and just format or print it. This is quite important when you have `extern(C++)` types where copy is expensive.
Even adding my own `toString` to it:
```
string toString() @safe pure nothrow @nogc { return "WhyNot"; }
```
does not make it compile.
Comment #1 by pro.mathias.lang — 2020-03-04T02:07:19Z
Note that the errors I'm getting right now, without the `toString`, are:
```
/usr/local/opt/dmd/include/dlang/dmd/std/stdio.d(1505): Error: struct foo.MyStruct is not copyable because it is annotated with @disable
/usr/local/opt/dmd/include/dlang/dmd/std/format.d(4813): Error: struct foo.MyStruct is not copyable because it is annotated with @disable
/usr/local/opt/dmd/include/dlang/dmd/std/format.d(494): Error: template instance std.format.getNthInt!("integer width", MyStruct) error instantiating
/usr/local/opt/dmd/include/dlang/dmd/std/stdio.d(1512): instantiated from here: formattedWrite!(LockingTextWriter, char, MyStruct)
/usr/local/opt/dmd/include/dlang/dmd/std/stdio.d(3870): instantiated from here: write!(MyStruct, char)
foo.d(15): instantiated from here: writeln!(MyStruct)
/usr/local/opt/dmd/include/dlang/dmd/std/format.d(494): Error: struct foo.MyStruct is not copyable because it is annotated with @disable
/usr/local/opt/dmd/include/dlang/dmd/std/format.d(508): Error: struct foo.MyStruct is not copyable because it is annotated with @disable
/usr/local/opt/dmd/include/dlang/dmd/std/format.d(4813): Error: struct foo.MyStruct is not copyable because it is annotated with @disable
/usr/local/opt/dmd/include/dlang/dmd/std/format.d(520): Error: template instance std.format.getNthInt!("integer precision", MyStruct) error instantiating
/usr/local/opt/dmd/include/dlang/dmd/std/stdio.d(1512): instantiated from here: formattedWrite!(LockingTextWriter, char, MyStruct)
/usr/local/opt/dmd/include/dlang/dmd/std/stdio.d(3870): instantiated from here: write!(MyStruct, char)
foo.d(15): instantiated from here: writeln!(MyStruct)
/usr/local/opt/dmd/include/dlang/dmd/std/format.d(520): Error: struct foo.MyStruct is not copyable because it is annotated with @disable
/usr/local/opt/dmd/include/dlang/dmd/std/format.d(531): Error: struct foo.MyStruct is not copyable because it is annotated with @disable
/usr/local/opt/dmd/include/dlang/dmd/std/format.d(4813): Error: struct foo.MyStruct is not copyable because it is annotated with @disable
/usr/local/opt/dmd/include/dlang/dmd/std/format.d(540): Error: template instance std.format.getNthInt!("separator digit width", MyStruct) error instantiating
/usr/local/opt/dmd/include/dlang/dmd/std/stdio.d(1512): instantiated from here: formattedWrite!(LockingTextWriter, char, MyStruct)
/usr/local/opt/dmd/include/dlang/dmd/std/stdio.d(3870): instantiated from here: write!(MyStruct, char)
foo.d(15): instantiated from here: writeln!(MyStruct)
/usr/local/opt/dmd/include/dlang/dmd/std/format.d(540): Error: struct foo.MyStruct is not copyable because it is annotated with @disable
/usr/local/opt/dmd/include/dlang/dmd/std/format.d(548): Error: struct foo.MyStruct is not copyable because it is annotated with @disable
/usr/local/opt/dmd/include/dlang/dmd/std/stdio.d(1512): Error: struct foo.MyStruct is not copyable because it is annotated with @disable
/usr/local/opt/dmd/include/dlang/dmd/std/stdio.d(3870): Error: struct foo.MyStruct is not copyable because it is annotated with @disable
foo.d(15): Error: struct foo.MyStruct is not copyable because it is annotated with @disable
```
Comment #2 by bugzilla — 2021-03-28T10:20:26Z
*** This issue has been marked as a duplicate of issue 9489 ***