Outdated egg!

This is an egg for CHICKEN 4, the unsupported old release. You're almost certainly looking for the CHICKEN 5 version of this egg, if it exists.

If it does not exist, there may be equivalent functionality provided by another egg; have a look at the egg index. Otherwise, please consider porting this egg to the current version of CHICKEN.

box

  1. Outdated egg!
  2. box
  3. Documentation
    1. Box syntax
    2. make-box
    3. make-box-variable
    4. make-box-location
    5. box?
    6. box-mutable?
    7. box-immutable?
    8. box-variable?
    9. box-location?
    10. box-set!
    11. box-ref
    12. box-location
    13. box-swap!
    14. make-box-variable-closure
    15. make-box-location-closure
    16. box
    17. set-box!
    18. unbox
  4. Usage
  5. Examples
  6. Notes
  7. Requirements
  8. Bugs and Limitations
  9. Author
  10. Version history
  11. License

Documentation

Implements the "box" data type. A box is a cell containing a single, mutable or immutable, field. The boxed value maybe any Scheme type, including a variable or a location.

Two APIs are provided, the original Chicken box API and a MzScheme/Gambit compatible API.

Box syntax

The lexical syntax of a box containing the object OBJECT is `#&OBJECT'.

Only mutable boxes can be recovered. An immutable, variable, or location box will lose identity when printed.

make-box

[procedure] (make-box [INITIAL [IMMUTABLE? #f]]) -> box

Returns a BOX with, optional, initial value INITIAL.

The BOX is mutable unless the IMMUTABLE? argument is #t.

An attempt to mutate an immutable box will signal an exception.

make-box-variable

[syntax] (make-box-variable VARIABLE [IMMUTABLE? #f]) -> box

Returns a boxed reference to the VARIABLE, which must be in lexical-scope.

The BOX is mutable unless the IMMUTABLE? argument is #t.

An attempt to mutate an immutable box will signal an exception.

make-box-location

[syntax] (make-box-location TYPE INITIAL-VALUE [IMMUTABLE? #f]) -> box

Returns a boxed reference to a location of TYPE and INITIAL-VALUE.

The BOX is mutable unless the IMMUTABLE? argument is #t.

An attempt to mutate an immutable box will signal an exception.

Unavailable in EVALuated source.

box?

[procedure] (box? OBJECT) -> boolean

Is OBJECT a BOX?

box-mutable?

[procedure] (box-mutable? OBJECT) -> boolean

Is OBJECT a mutable BOX?

box-immutable?

[procedure] (box-immutable? OBJECT) -> boolean

Is OBJECT an immutable BOX?

box-variable?

[procedure] (box-variable? OBJECT) -> boolean

Is OBJECT a boxed variable?

box-location?

[procedure] (box-location? OBJECT) -> boolean

Is OBJECT a boxed location?

box-set!

[procedure] (box-set! BOX OBJECT)

Changes the boxed value of BOX to OBJECT. Will signal an exception for an immutable {{BOX}.

box-ref

[procedure] (box-ref BOX) -> *

Returns the boxed value of BOX.

box-location

[procedure] (box-location BOX [WEAK? #f]) -> location

Returns a LOCATION object for a boxed variable, location or locatable box. Signals an exception otherwise.

The locative is "strong" unless the WEAK? argument is #t. The WEAK? argument is ignored for boxed variables and locations.

The location of a boxed value or boxed location is the box. The location of a boxed variable is the same as (location (box-ref BOX)); currently the location of a symbol may not be taken.

See Locations.

box-swap!

[procedure] (box-swap! BOX FUNC [OBJECT...])

Changes the boxed value of BOX to (FUNC <BOX-VALUE> OBJECT...). Will signal an exception for an immutable BOX}. Returns the new value of {{BOX.

make-box-variable-closure

[procedure] (make-box-variable-closure IMMUTABLE? REF SET) -> box
IMMUTABLE?
boolean
REF
(-> *)
SET
(* -> void)

make-box-location-closure

[procedure] (make-box-location-closure IMMUTABLE? REF SET REFLOC) -> box
IMMUTABLE?
boolean
REF
(-> *)
SET
(* -> void)
REFLOC
(-> location)

box

[procedure] (box OBJECT) -> box

Returns a mutable BOX with initial value OBJECT.

set-box!

[procedure] (set-box! BOX OBJECT)

Changes the boxed value of BOX to OBJECT. Will signal an exception for an immutable {{BOX}.

unbox

[procedure] (unbox BOX) -> *

Returns the boxed value of BOX.

Usage

(require-extension box)

or

(require-library box)
...
(import box)

Examples

#;1> (define b (box 0))
#;2> b
#&0
#;3> (define (inc-box! bx) (set! (box-ref bx) (add1 (unbox bx))))
#;4> (inc-box! b)
#;5> (unbox b)
1
#;6> (box-swap! b add1)
2
#;7> (box-swap! b * 3)
6
#;8> (box-ref b)
6

Notes

Requirements

Unit lolevel

Bugs and Limitations

The location of a boxed variable is rather useless.

Author

Kon Lovett

Version history

2.4.0
Add make-box-variable-closure & make-box-location-closure.
2.3.3
Updated type information.
2.3.2
Added type information.
2.3.1
2.3.0
Added box-swap!. Fix for print of mutable box.
2.2.4
Bug fix for Chicken 4.6.3
2.2.3
2.2.2
2.2.1
2.1.0
Bug fix for box?. Added direct calls (only for use when the actual type is known.)
2.0.0
Port to hygienic Chicken. Reduced memory footprint for basic usage. Dropped keyword arguments.

License

Copyright (C) 2009-2018 Kon Lovett. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ASIS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.