On Thursday, 12 August 2021 at 12:10:49 UTC, Learner wrote:

That worked fine, but the codebase is @safe:

```d
cast from `int[]` to `inout(int[])` not allowed in safe code
```

So copy constructors force me to introduce trusted methods, while that was not necessary with postblits?

A postblit would simply ignore the type qualifier--which can lead to undefined behavior. (Scroll down to the paragraph that begins "An unqualified postblit..." under ["Struct Postblits"][1] in the spec.) The copy constructor merely forces you to be honest about the safety of your code.

In your case, I would recommend encapsulating the unsafe cast in a function like the following:

```d
T[] dupWithQualifiers(T[] array)
{
    auto copy = array.dup;
    return (() @trusted => cast(T[]) copy)();
}
```

You can then use this function in place of `dup` in your copy constructor.

[1]: https://dlang.org/spec/struct.html#struct-postblit

Reply via email to