Next: Lazy evaluation, Previous: Parameters and dynamic states, Up: Core library [Contents][Index]
A box is a mutable container that can hold (possibly multiple) values. It can be used as a minimal data storage, or a sort of mutable indirect “pointer”.
Traditionally, a list or a vector has been used for this purpose. However, such datatypes imply sequences of objects, Using boxes emphasizes your intention is just for indirection, and not so much for sequencing.
It is originally introduced by SRFI-111, which was later adopted in
R7RS-large as scheme.box
. SRFI-195 enhances it to deal with
multiple values.
The srfis leave some details to implementations. Here are our choices:
equal?
compares their contents when
two are not eqv?
. In the spec, when two boxes are eqv?
then they must also be equal?
to each other, but it’s up
to the implementation when two are not eqv?
.
When you’re writing portable code, be careful not to depend on the
equal?
behavior.
[R7RS box][SRFI-195]
Returns a fresh box object that contains the value val
….
[R7RS box]
Returns #t
iff obj is a box object.
[SRFI-195] Returns the number of values box holds.
[R7RS box] Returns box’s content. If box has N values, it returns N values.
NB: If box is a single-valued box, unbox
can be used
with generalized setters. This is Gauche’s extension.
(define b (box 1)) (set! (unbox b) 2) b ⇒ #<box 2> (define b (box '())) (push! (unbox b) 'x) (push! (unbox b) 'y) b ⇒ #<box (y x)>
[SRFI-195] Returns i-th value held in box.
NB: This can be used with generalized setters. This is Gauche’s extension.
(define mb (box 10 20 30)) (inc! (unbox-value mb 1) mb ⇒ #<mv-box[3] 10 21 30>
[R7RS box][SRFI-195] Alters the content of box with val …. The numer of values must match the arity of the box. Returns unspecified value.
[SRFI-195] Alters i-th value of box with val. Returns unspecified value.
Next: Lazy evaluation, Previous: Parameters and dynamic states, Up: Core library [Contents][Index]