References
Pre-releaseThis release adds (tentative) support for references via functions.
References can be used to indicate an object without preventing it from being garbage collected by the virtual machine.
Two types of reference are supported in this version:
weak_reference(object)
soft_reference(object)
Weak references point to an object but do not prevent it from being garbage-collected. If there are no other (strong) references to it, the object will be cleared during garbage collection and the reference's value will be null
.
Weak references are useful for pointing to information provided by something else, without having to keep track of when it needs to be disposed (e.g. a reference to a Minecraft Player
.)
Soft references also do not prevent garbage-collection, but encourage the virtual machine to hold on to the object where possible (e.g. until memory needs to be freed.)
The implementation differs between machines: client java distributions typically hold on to soft references for as long as possible, whereas server distributions will dispose of them more readily.
Soft references are useful for caching information that can be re-created but is inexpensive to hold on to, (e.g. a parsed configuration file or json structure.) The value can disappear without warning but typically only when the machine needs to free up space.
The (potential) value can be extracted using:
reference_value(reference)
References may be supported at a language level in the future. There is a (tentative) plan to support them when variables V2 is finished.