Find struct not passed by reference
jfondren
julian.fondren at gmail.com
Mon Aug 2 23:19:48 UTC 2021
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;
}
```
More information about the Digitalmars-d-learn
mailing list