On Monday, 2 August 2021 at 23:06:42 UTC, frame wrote:
Is there a way to find a struct which should be passed by reference but accidentally isn't? Maybe with copy constructors?

@disable postblit:

```d
struct NoCopy {
    int n;
    @disable this(this);
}

void modify(NoCopy nc) {
    nc.n++;
}

void main() {
    NoCopy x;
x.modify; // Error: struct `catchcopy.NoCopy` is not copyable ...
}
```

std.typecons.Unique:

```d
import std.typecons : Unique;

class Val {
    int n;
}

void incr(Unique!Val v) {
    v.n++;
}

void decr(ref Unique!Val v) {
    v.n--;
}

void show(Unique!Val v) {
    import std.stdio : writeln;

    writeln(v.n);
}

void main() {
    Unique!Val x = new Val;
    // x.incr; // Error: ... is not copyable
    x.decr;
    x.release.show;
}
```

Reply via email to