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

Paul Backus snarwin at gmail.com
Tue Sep 28 16:37:53 UTC 2021


On Tuesday, 28 September 2021 at 06:09:19 UTC, FeepingCreature 
wrote:
> Yes, in a real implementation of Turducken (not just a concept 
> demo), `value` would be `private` and not ref readable. It's 
> purely the `ref T value` that ruins the const system, but 
> that's not a necessary or even desired part of my feature set. 
> This is why I'm also not interested in the current "Rebindable 
> for structs" PR, because `Rebindable` exposes its value by ref 
> and is thus inherently unsafe for exactly the reason you 
> describe.
>
> Hence the thing I asked for was "the value to be returned from 
> a (non-ref) function", an operation which copies exactly the 
> parts of the value that head-mutable makes mutable. ("head" == 
> "by-value", pretty much exactly.)

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


More information about the Digitalmars-d mailing list