Weak pairs are a mechanism for building data structures that point at objects without protecting them from garbage collection. The car of a weak pair holds its pointer weakly, while the cdr holds its pointer in the normal way. If the object in the car of a weak pair is not held normally by any other data structure, it will be garbage-collected.
Note: weak pairs are not pairs; that is, they do not satisfy the
This predicate returns
#fif the car of weak-pair has been garbage-collected; otherwise returns
#t. In other words, it is true if weak-pair has a valid car component.
weak-pair/car? is used to determine if
would return a valid value. An obvious way of doing this would be:
(if (weak-pair/car? x) (weak-car x) ...)
However, since a garbage collection could occur between the call to
weak-car, this would not always work
correctly. Instead, the following should be used, which always works:
(or (weak-car x) (and (not (weak-pair/car? x)) ...))
The reason that the latter expression works is that
#f in just two instances: when the car component is
#f, and when the car component has been garbage-collected. In
the former case, if a garbage collection happens between the two calls,
it won't matter, because
#f will never be garbage-collected. And
in the latter case, it also won't matter, because the car component no
longer exists and cannot be affected by the garbage collector.
Sets the car component of weak-pair to object and returns an unspecified result.