I wrote an AR archive (.a files) parser in D

IchorDev zxinsworld at gmail.com
Sat Aug 10 07:55:53 UTC 2024


On Saturday, 10 August 2024 at 00:44:29 UTC, Paul Backus wrote:
> 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

Yes, it also says so in [section 2 
here](https://dlang.org/spec/const3.html#creating_immutable_data). However, the function doesn’t guarantee or even promise that it returns the only mutable reference, so using an explicit cast cannot be guaranteed to be safe without manual verification. I think `read`’s POSIX implementation does return the only mutable reference, but I’m not that familiar with POSIX functions.

> 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

Good point, I forgot about that.


More information about the Digitalmars-d mailing list