Creating immutable arrays in @safe code
ag0aep6g
anonymous at example.com
Sat Jul 17 05:44:24 UTC 2021
On 17.07.21 00:27, H. S. Teoh wrote:
> Hmm, OK. Not sure why .array isn't being inferred as unique... but yeah,
> you probably have to resort to using @trusted with .assumeUnique.
In addition to `pure`, you also need a const/immutable input and a
mutable output, so that the output cannot be a slice of the input.
For std.array.array it might be possible to carefully apply `Unqual` to
the element type.
I tried doing that, but `-preview=dip1000` causes trouble. This fails:
----
int[] array(const int[] input) pure nothrow @safe
{
int[] output;
foreach (element; input) output ~= element;
return output;
}
void main() pure nothrow @safe
{
const int[] c = [1, 2, 3];
immutable int[] i = array(c);
/* Without `-preview=dip1000`: works, because the result is unique.
With `-preview=dip1000`: "Error: cannot implicitly convert". */
}
----
I'm not sure what's going on. `pure` being involved makes me think of
issue 20150. But it also fails with my fix for that issue. So maybe it's
another bug.
More information about the Digitalmars-d-learn
mailing list