-
-
Notifications
You must be signed in to change notification settings - Fork 705
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
writeln of struct with disabled copy ctor #9950
Labels
Comments
andrej.mitrovich (@AndrejMitrovic) commented on 2013-02-09T05:44:27ZUnrelated to scoped classes, the root cause is that writeln() takes its arguments by value:
import std.stdio;
struct S
{
@disable this(this);
}
void main()
{
S s;
writeln(s); // error
} |
andrej.mitrovich (@AndrejMitrovic) commented on 2013-02-18T20:10:35ZAs a workaround you can use this:
import std.stdio;
import std.string;
struct Wrap(T)
{
T* t;
auto ref opDispatch(string s, T...)(T args)
{
mixin(format("return t.%s(args);", s));
}
}
auto wrap(T)(T* t)
{
return Wrap!T(t);
}
struct S
{
@disable this(this);
int x;
float y;
string toString()
{
return format("S { x: %s, y: %s }", x, y);
}
}
void main()
{
S s = S(1, 2.0);
writeln(wrap(&s));
}
(std.typecons.Proxy doesn't seem to help here, it won't work with pointers)
Generally speaking this is a gigantic problem to fix. Pretty much everything in Phobos expects structs to be copyable. Phobos could try to take arguments by ref (or rather auto ref for compatibility), but this would have to be applied to all functions.
I think the safest thing we can do is ask the user to use this sort of wrapper type.
Maybe writeln/format could be the exception to the rule and take all arguments by auto ref and then internally use wrappers for @disable structs to pass them to other internal formatting functions. This would make it convenient to print @disable structs since it's common to print things.. |
hsteoh commented on 2013-03-15T10:58:55ZThis issue is not just limited to printing. Structs that have @disabled this(this) are currently highly-crippled, because almost everything in Phobos assumes that structs can be passed by value. A struct range with @disabled this(this) is unusable with any range function unless you do something really ugly like (&range).cycle(), etc., which is a kind of hack (taking advantage of D's automatic dereference when a pointer is used with the . operator).
Auto ref would solve a lot of these problems. |
bugzilla (@WalterBright) commented on 2021-03-28T10:20:26Z*** Issue 20632 has been marked as a duplicate of this issue. *** |
dlang-bot commented on 2021-05-08T14:19:49Z@berni44 created dlang/phobos pull request #8055 "Fix partly Issue 9489 - writeln of struct with disabled copy ctor" mentioning this issue:
- Fix partly Issue 9489 - writeln of struct with disabled copy ctor
https://github.com/dlang/phobos/pull/8055 |
dlang-bot commented on 2021-05-12T07:42:06Zdlang/phobos pull request #8055 "Fix Issue 9489 - writeln of struct with disabled copy ctor" was merged into master:
- ef30be7c7376cb4aef99d851234c1197f290cea7 by berni44:
Fix Issue 9489 - writeln of struct with disabled copy ctor
https://github.com/dlang/phobos/pull/8055 |
dlang-bot commented on 2021-05-18T06:49:46Zdlang/phobos pull request #8092 "Revert "Fix Issue 9489 - writeln of struct with disabled copy ctor"" was merged into master:
- 116d33b63516c596f85b84793812cd97cb37578d by Iain Buclaw:
Revert "Fix Issue 9489 - writeln of struct with disabled copy ctor"
This reverts commit 838da632cbaf4a1888dd7a76754be9fb50520e25.
https://github.com/dlang/phobos/pull/8092 |
ibuclaw (@ibuclaw) commented on 2021-05-18T07:46:45ZFix was reverted. |
dlang-bot commented on 2021-05-18T22:54:00Z@dkorpel updated dlang/phobos pull request #8066 "improve documentation of SortedRange.release" mentioning this issue:
- Revert "Fix Issue 9489 - writeln of struct with disabled copy ctor"
This reverts commit 838da632cbaf4a1888dd7a76754be9fb50520e25.
https://github.com/dlang/phobos/pull/8066 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
bearophile_hugs reported this on 2013-02-09T04:52:58Z
Transfered from https://issues.dlang.org/show_bug.cgi?id=9489
CC List
Description
import std.stdio: writeln; import std.conv: text; import std.typecons: scoped; class Foo { int x; this(int x_) { this.x = x_; } override string toString() { return text(x); } } void main() { auto f = scoped!Foo(100); writeln(f.x); // OK writeln(f); // Error typeof(scoped!Foo(1))[10] foos; foreach (i, ref fi; foos) fi.x = i * 10; writeln(foos); // Error } dmd 2.062beta gives: ...\dmd2\src\phobos\std\stdio.d(709): Error: struct std.typecons.scoped!(Foo, int).scoped.Scoped!(Foo).Scoped is not copyable because it is annotated with @disable ...\dmd2\src\phobos\std\format.d(414): Error: struct std.typecons.scoped!(Foo, int).scoped.Scoped!(Foo).Scoped is not copyable because it is annotated with @disable ...\dmd2\src\phobos\std\format.d(437): Error: struct std.typecons.scoped!(Foo, int).scoped.Scoped!(Foo).Scoped is not copyable because it is annotated with @disable ...\dmd2\src\phobos\std\format.d(451): Error: struct std.typecons.scoped!(Foo, int).scoped.Scoped!(Foo).Scoped is not copyable because it is annotated with @disable ...\dmd2\src\phobos\std\format.d(463): Error: struct std.typecons.scoped!(Foo, int).scoped.Scoped!(Foo).Scoped is not copyable because it is annotated with @disable ...\dmd2\src\phobos\std\format.d(475): Error: struct std.typecons.scoped!(Foo, int).scoped.Scoped!(Foo).Scoped is not copyable because it is annotated with @disable ...\dmd2\src\phobos\std\format.d(489): Error: struct std.typecons.scoped!(Foo, int).scoped.Scoped!(Foo).Scoped is not copyable because it is annotated with @disable ...\dmd2\src\phobos\std\format.d(498): Error: struct std.typecons.scoped!(Foo, int).scoped.Scoped!(Foo).Scoped is not copyable because it is annotated with @disable ...\dmd2\src\phobos\std\stdio.d(714): Error: template instance std.format.formattedWrite!(LockingTextWriter, char, Scoped!(Foo)) error instantiating ...\dmd2\src\phobos\std\stdio.d(1620): instantiated from here: write!(Scoped!(Foo),char) temp.d(12): instantiated from here: writeln!(Scoped!(Foo)) ...\dmd2\src\phobos\std\stdio.d(714): Error: struct std.typecons.scoped!(Foo, int).scoped.Scoped!(Foo).Scoped is not copyable because it is annotated with @disable ...\dmd2\src\phobos\std\stdio.d(1620): Error: template instance std.stdio.File.write!(Scoped!(Foo),char) error instantiating temp.d(12): instantiated from here: writeln!(Scoped!(Foo)) ...\dmd2\src\phobos\std\stdio.d(1620): Error: struct std.typecons.scoped!(Foo, int).scoped.Scoped!(Foo).Scoped is not copyable because it is annotated with @disable temp.d(12): Error: template instance std.stdio.writeln!(Scoped!(Foo)) error instantiating temp.d(12): Error: struct std.typecons.scoped!(Foo, int).scoped.Scoped!(Foo).Scoped is not copyable because it is annotated with @disable ...\dmd2\src\phobos\std\format.d(1821): Error: static assert (isInputRange!(Scoped!(Foo)[])) is false ...\dmd2\src\phobos\std\format.d(1789): instantiated from here: formatValue!(LockingTextWriter, Scoped!(Foo)[], char) ...\dmd2\src\phobos\std\format.d(2953): instantiated from here: formatValue!(LockingTextWriter, Scoped!(Foo)[10u], char) ...\dmd2\src\phobos\std\format.d(416): instantiated from here: formatGeneric!(LockingTextWriter, Scoped!(Foo)[10u], char) ...\dmd2\src\phobos\std\stdio.d(735): ... (1 instantiations, -v to show) ... ...\dmd2\src\phobos\std\stdio.d(1620): instantiated from here: write!(Scoped!(Foo)[10u],char) temp.d(16): instantiated from here: writeln!(Scoped!(Foo)[10u])The text was updated successfully, but these errors were encountered: