chickadee » chicken » base » weak-cons

(weak-cons obj[1] obj[2])procedure

Returns a newly allocated weak pair whose car is obj[1] and whose cdr is obj[2]. The pair is indistinguishable from normal pairs, except as noted above.

(weak-cons 'a '())                   ===>  (a)
(weak-cons '(a) '(b c d))            ===>  ((a) b c d)
(weak-cons "a" '(b c))               ===>  ("a" b c)
(weak-cons 'a 3)                     ===>  (a . 3)
(weak-cons '(a b) 'c)                ===>  ((a b) . c)

(import (chicken gc))

(let* ((x '(a b))
       (y (weak-cons x 'c)))
  (gc #t)
  (car x))                           ===> (a b)

(let ((x (weak-cons '(a b) 'c)))
  (gc #t)
  (car x))                           ===>  #!bwp

As the final two examples show, when something else still holds on to the value that's stored in the car of a weak pair, it will not be reclaimed. But if the value is only referenced by one or more weak pairs, it is reclaimed and the car of the weak pair is replaced with the broken-weak-pointer value #!bwp.