Will D always have a way to rebind an arbitrary data type?

FeepingCreature feepingcreature at gmail.com
Wed Sep 29 07:27:50 UTC 2021


On Tuesday, 28 September 2021 at 16:37:53 UTC, Paul Backus wrote:
> This sounds like basically the same thing as Rust's [`Cell`][1] 
> type.
>
> I think to implement this safely it might be necessary to have 
> the storage typed as mutable (something like `union { Unconst!T 
> payload; }`) and cast to/from `T` in `get` and `set`, since 
> otherwise `set` invokes UB by [mutating memory typed as `const` 
> or `immutable`][2]. Other than that, I don't see any problem.
>
> [1]: https://doc.rust-lang.org/std/cell/
> [2]: https://dlang.org/spec/const3.html#removing_with_cast

Now that you mention it, this causes a problem: it's 
**impossible** to have correctly typed memory here in D as it 
stands. Unqual/Unconst is *not* enough, because structs can have 
const fields, a state that cannot be stripped by any means. And 
`void[sizeof(T)]` breaks garbage collector typing.

The only recourse I can see offhand, without language changes, 
would be to laborously build an "equivalent but mutable type" out 
of `void[size_t.sizeof]` and `ubyte[size_t.sizeof]` fragments.

Or ignore that UB and hope it doesn't matter.

Ideas? Options? Any hope at all?


More information about the Digitalmars-d mailing list