D doesn't have weak references. So how can I make a associative array of objects without preventing their destruction?

evilrat evilrat666 at gmail.com
Fri May 10 15:04:30 UTC 2024


On Friday, 10 May 2024 at 13:27:40 UTC, Dukc wrote:
> Steven Schveighoffer kirjoitti 10.5.2024 klo 16.01:
>> On Friday, 10 May 2024 at 11:05:28 UTC, Dukc wrote:
>>> This also gets inferred as `pure` - meaning that if you use 
>>> it twice for the same `WeakRef`, the compiler may reuse the 
>>> result of the first dereference for the second call, without 
>>> checking whether the referred value has changed!
>> 
>> This would be weak pure since the reference is mutable. This 
>> cannot be memoized.
>
> The difference is the type. With a pointer parameter (both a 
> bare one and one in a struct), the compiler can cache the 
> result only when the pointed data is similar. However, here we 
> have an integer parameter. It can be cached if it itself is 
> similar to the one in the other function call. The compiler 
> doesn't have to know, nor can know, when a `size_t` is a 
> pointer in disguise.

This why I would just use ref counting if I were the topic 
author, trying to be smart will often comes back when one doesn't 
expect.

And as stated by previous author if the goal is to have 
self-clearable weak reference it will need some infrastructure 
anyway, so tbh this will greatly outweight any possible benefits 
of having weak refs.


More information about the Digitalmars-d-learn mailing list