Want reasonable reference counting? Disable automatic sharing of immutable

Paul Backus snarwin at gmail.com
Fri Nov 12 19:55:08 UTC 2021


On Friday, 12 November 2021 at 19:22:57 UTC, tsbockman wrote:
> Just track whether an RC target needs atomic RC operations at 
> compile time, using the template parameters of RC types. No 
> changes to language semantics are necessary.
>
> For example, reject attempts to assign an `RCPtr!(immutable T)` 
> to an `RCPtr!(const T)`, while allowing assignment to a 
> `RCPtr!(shared const T)`. Borrowed payloads can and should 
> follow the normal D qualifier conversion rules.
>
> Or, if more granularity is needed, use a separate template 
> parameter:
>
> ```D
> struct RCPtr(T, bool sharedMeta = is(T == shared))
> ```

This is essentially what Rust does--except instead of a template 
parameter, they have two separate generic types, [`Rc<T>`][1] and 
[`Arc<T>`][2].

[1]: https://doc.rust-lang.org/std/rc/struct.Rc.html
[2]: https://doc.rust-lang.org/std/sync/struct.Arc.html


More information about the Digitalmars-d mailing list