[Pharo-dev] Weak pointer
camille.teruel at gmail.com
Thu Apr 10 04:59:56 EDT 2014
On 10 avr. 2014, at 10:50, Yuriy Tymchuk <yuriy.tymchuk at me.com> wrote:
> Hi guys.
> Is there a way to have a week pointer? Or some wrapper with a week pointer? I know that there are week collections, but I what to have a “variable” that won’t prevent the object it points to be garbage collected.
If a class is weak it is also variable. So you have basically two solutions.
If your variable is an instance variable, you can declare that your class is weak and initialize it with a size of 1, the fixed fields will remain strong, only the indexed field will be weak.
You can have a look at WeakKeyAssociation and WeakValueAssociation for examples.
Otherwise, you can wrap your variable into a weak array of size 1. Later slots will be able to do the job transparently.
More information about the Pharo-dev