I wrote an AR archive (.a files) parser in D
Paul Backus
snarwin at gmail.com
Sat Aug 10 00:44:29 UTC 2024
On Thursday, 8 August 2024 at 11:07:35 UTC, IchorDev wrote:
> On Thursday, 8 August 2024 at 10:44:49 UTC, Renato Athaydes
> wrote:
>> Why shouldn't I cast the `read` result directly to immutable
>> when it's literally impossible to get a reference to those
>> bytes that is not immutable? It seems like the best way to
>> achieve what I need.
>
> You’re still violating the guarantees of immutability—data
> can’t go from mutable to immutable, that’s what `const` is for.
> Using `const` would be totally fine, or you can use `.idup` to
> create an immutable copy of the array.
Strictly speaking, you are allowed to cast from const/mutable to
immutable if there is only one reference to the data in question:
> A memory location can be transferred from thread-local to
> immutable or shared if there is only one reference to the
> location.
Source: https://dlang.org/spec/intro.html#memory-model
The recommended way to do this is to use the library function
[`assumeUnique`][1], since that communicates the intent of the
code better than a naked cast.
[1]: https://dlang.org/library/std/exception/assume_unique.html
More information about the Digitalmars-d
mailing list