www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 22077] New: `std.sumtype` support for copy constructors is


          Issue ID: 22077
           Summary: `std.sumtype` support for copy constructors is
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: pro.mathias.lang gmail.com

The following code does not compile:
import std.sumtype;

struct Struct
    int foobar;

    this (int value)  system { this.foobar = value; }
    this (const ref Struct value) inout  system { this.foobar = value.foobar; }

void main ()  safe
    Struct n;
    SumType!(Struct, int) x = n;

Error message as of v2.097.0:
oops.d(15): Error: none of the overloads of `this` are callable using argument
types `(Struct)`, candidates are:
std/sumtype.d(364):        `std.sumtype.SumType!(Struct,
int).SumType.this(Struct value)`
std/sumtype.d(364):        `std.sumtype.SumType!(Struct, int).SumType.this(int
std/sumtype.d(395):        `std.sumtype.SumType!(Struct,
int).SumType.this(const(Struct) value)`
std/sumtype.d(413):        `std.sumtype.SumType!(Struct,
int).SumType.this(immutable(Struct) value)`
std/sumtype.d(395):        `std.sumtype.SumType!(Struct,
int).SumType.this(const(int) value)`
oops.d(15):        ... (2 more, -v to show) ...

Making the copy ctor ` safe` works, but it means that an extra copy is
Additionally, if the copy constructor is defined as:
    this (const ref Struct value)  safe { this.foobar = value.foobar; }
(So, not `inout`), it will fail with the following error:
std/sumtype.d-mixin-417(417): Error: none of the overloads of `__ctor` are
callable using a `immutable` object, candidates are:
oops.d(8):        `oops.Struct.this(int value)`
oops.d(9):        `oops.Struct.this(ref const(Struct) value)`
Error: constructor `std.sumtype.SumType!(Struct, int).SumType.this` label
`__returnLabel` is undefined
oops.d(15): Error: template instance `std.sumtype.SumType!(Struct, int)` error

Jun 22 2021