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

6.17 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))
gosh> (weak-vector-ref v 0)
gosh> (weak-vector-set! v 0 (cons 1 1))
gosh> (weak-vector-ref v 0)
(1 . 1)
gosh> (gc)
gosh> (gc)
gosh> (weak-vector-ref v 0)
Builtin Class: <weak-vector>

The weak vector class. Inherits <sequence> and <collection>, so you can use gauche.collection (See section gauche.collection - Collection framework) and gauche.sequence (See section 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.

If the element has been garbage collected, this procedure returns fallback if it is provided, #f otherwise.

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 ] [ >> ]

This document was generated on July 19, 2014 using texi2html 1.82.