[Issue 9489] writeln of struct with disabled copy ctor

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Feb 18 20:10:39 PST 2013


http://d.puremagic.com/issues/show_bug.cgi?id=9489



--- Comment #2 from Andrej Mitrovic <andrej.mitrovich at gmail.com> 2013-02-18 20:10:35 PST ---
As 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..

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list