I wrote an AR archive (.a files) parser in D
Renato Athaydes
renato at athaydes.com
Thu Aug 8 10:44:49 UTC 2024
On Tuesday, 6 August 2024 at 14:55:50 UTC, IchorDev wrote:
> On Sunday, 4 August 2024 at 20:14:50 UTC, Renato wrote:
>> Just announcing here in case someone may be interested in that
>> (the code is very easy to read) and wants to give some
>> feedback on my usage of D
>
> First of all, you should really be using `const` instead of
> `in`.
> Second, do not cast from `const(ubyte[])` to `string`. This is
> a violation of D’s type system. `string` is an alias of
> `immutable(char)[]`. You’re casting to `immutable`—never cast
> to `immutable`! Immutable data must never change during the
> lifetime of a program, but `const` data may be changed from
> elsewhere (e.g. another thread), so this cast violates all the
> assumptions we make for immutable data! Instead, you should
> cast to `const(char[])` in those places.
> You also cast to immutable
> [here](https://github.com/renatoathaydes/dar/blob/364494cfa18c884621c9ea92a800e281e9e4daac/source/dar.d#L122).
>
> Since both mutable and immutable data implicitly cast to
> `const`, if you want to take both as parameters, then always
> make your parameters `const`.
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.
Also, I thought `in` is the new way of using `const` parameters,
and they're equivalent according to the docs.
More information about the Digitalmars-d
mailing list