[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

6.14 Weak pointers

A weak pointer is a reference to an object that doesn't prevent the object from being garbage-collected. Gauche provides weak pointers as a weak vector object. A weak vector is like a vector of objects, except each object can be garbage collected if it is not referenced from objects other than weak vectors. If the object is collected, the entry of the weak vector is replaced for #f.
 
gosh> (define v (make-weak-vector 1))
v
gosh> (weak-vector-ref v 0)
#f
gosh> (weak-vector-set! v 0 (cons 1 1))
#<undef>
gosh> (weak-vector-ref v 0)
(1 . 1)
gosh> (gc)
#<undef>
gosh> (gc)
#<undef>
gosh> (weak-vector-ref v 0)
#f

Builtin Class: <weak-vector>
The weak vector class. Inherits <sequence> and <collection>, so you can use gauche.collection (See section 9.3 gauche.collection - Collection framework) and gauche.sequence (See section 9.18 gauche.sequence - Sequence framework).
 
(coerce-to <weak-vector> '(1 2 3 4))
  => a weak vector with four elements

Function: make-weak-vector size
Creates and returns a weak vector of size size.

Function: weak-vector-length wvec
Returns the length of a weak vector wvec.

Function: weak-vector-ref wvec k &optioal fallback
Returns k-th element of a weak vector wvec.

By default, weak-vector-ref signals an error if k is negative, or greater than or equal to the size of wvec. However, if an optional argument fallback is given, it is returned for such case.

With gauche.sequence module, you can also use a method ref.

Function: weak-vector-set! wvec k obj
Sets k-th element of the weak vector wvec to obj. It is an error if k is negative or greater than or equal to the size of wec.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated by Ken Dickey on November, 28 2002 using texi2html