[Issue 22077] New: `std.sumtype` support for copy constructors is incomplete

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Jun 23 03:50:11 UTC 2021


https://issues.dlang.org/show_bug.cgi?id=22077

          Issue ID: 22077
           Summary: `std.sumtype` support for copy constructors is
                    incomplete
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: phobos
          Assignee: nobody at puremagic.com
          Reporter: pro.mathias.lang at 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
value)`
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
incurred.
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
instantiating
```

--


More information about the Digitalmars-d-bugs mailing list