ID:2662462
 
Applies to:DM Language
Status: Open

Issue hasn't been assigned a status value.
Over on /tg/, we have a /datum/weakref class: https://github.com/tgstation/tgstation/blob/master/code/ datums/weakrefs.dm

This allows for weak references, AKA references to other datums without counting as strong references. In the code, this means that when an object is destroyed, the next time the weakref is resolve()'d, it returns null.

This is good for non-owning relationships (such as the ability to create weak `owner` variables inside children) without much overhead, while also creating an explicit contract as to what the variable is for.

However, our implementation has some limitations.

Firstly, it does not have any ability to support types. We must manually re-cast the type every time it is used.

Second, it most likely takes up significantly more memory than a normal ref.

I would like to see official weakrefs be added to BYOND, as it would solve both these issues while ideally letting us preserve the good.
This would be a very useful vanilla addition for better GC
Pointers seem to do this okay so I am sated
Nevermind, they are only references to the specific name and not to the object:

/datum/weakref
var/reference

/datum/weakref/New(datum/thing)
reference = &thing

/datum/weakref/proc/resolve()
return *reference

/mob
var/datum/weakref/ref_to_myself

/mob/verb/create_ref()
var/myself = src
ref_to_myself = new(myself)

/mob/verb/resolve_ref()
var/should_be_myself = ref_to_myself.resolve()
world.log << "i am [should_be_myself || "null"]"


create_ref() then resolve_ref() gives null because its a reference to "myself" and not "src"