On Thursday, 15 July 2021 at 11:08:25 UTC, Per Nordlöw wrote:
The adding of copy construtors to `Service` defined as
```d
@safe struct Service {
this(ref return scope typeof(this) rhs) {}
this(const ref return scope typeof(this) rhs) const {}
}
@safe struct Session {
void openAndGetService(in string key) scope {
import std.algorithm.searching : find;
auto hit = _pairs.find!((x) => x.key == key)();
}
private Pair[] _pairs;
private struct Pair {
string key;
Service service;
}
}
```
struct Pair will have an inout(inout) copy constructor defined
the following way:
this(inout ref return scope typeof(this) rhs) inout
{
key = rhs.key;
service = rhs.service;
}
`service = rhs.service` will be rewritten to
service.__cpctor(rhs.service), but since Service does not define
an inout copy constructor it will fail to typecheck and the
compiler will annotate it with @disable. Therefore, Pair will
become uncopyable. This will result in isInputRange failing on
Pair[] (more specifically, this constraints from isInputRange:
is(typeof((return ref R r) => r.front)). If the copy constructor
of Service is inout(inout), the generated copy constructor of
Pair will be succesfully typechecked and everything will work.
For more context, please see this issue [1] and more
specifically, this comment [2].
Cheers,
RazvanN
[1] https://issues.dlang.org/show_bug.cgi?id=20876
[2] https://issues.dlang.org/show_bug.cgi?id=20876#c4