5. Bigloo -- Standard Library 5.3 Structures and Records

Browsing

Home: | Bigloo A ``practical Scheme compiler'' User manual for version 2.6b December 2003
Previous chapter: | Core Language
Next chapter: | Pattern Matching

Standard Library

5.1 | Scheme Library
| 5.1.1 | Booleans
| 5.1.2 | Equivalence predicates
| 5.1.3 | Pairs and lists
| 5.1.4 | Symbols
| 5.1.5 | Keywords
| 5.1.6 | Numbers
| 5.1.7 | Characters
| 5.1.8 | UCS-2 Characters
| 5.1.9 | Strings
| 5.1.10 | Unicode (UCS-2) Strings
| 5.1.11 | Vectors
| 5.1.12 | Control features
5.2 | Input and output
5.3 | Structures and Records
| 5.3.1 | Structures
| 5.3.2 | Records (SRFI-9)
5.4 | Serialization
5.5 | Bit manipulation
5.6 | Hash Tables
| 5.6.1 | Hash tables
| 5.6.2 | Deprecated Hash tables
5.7 | System programming
| 5.7.1 | Operating System interface
| 5.7.2 | Files
5.8 | Process support
5.9 | Socket support
5.10 | Date
5.11 | Posix Regular Expressions
| 5.11.1 | Regular Expressions Procedures
| 5.11.2 | Regular Expressions Pattern Language
| 5.11.3 | An Extended Example

Chapters

| Acknowledgements
1. | Table of contents
2. | Overview of Bigloo
3. | Modules
4. | Core Language
5. | Standard Library
6. | Pattern Matching
7. | Object System
8. | Threads
9. | Regular parsing
10. | Lalr(1) parsing
11. | Errors and Assertions
12. | Eval and code interpretation
13. | Macro expansion
14. | Command Line Parsing
15. | Explicit typing
16. | The C interface
17. | The Java interface
18. | Bigloo Libraries
19. | Extending the Runtime System
20. | SRFIs
21. | DSSSL support
22. | Compiler description
23. | User Extensions
24. | Bigloo Development Environment
25. | Global Index
26. | Library Index
| Bibliography

Bigloo supports two kinds of enumerated types: the structures and the records. They offer similar facilities. Structures where pre-exising to records and they are maintained mainly for backward compatiblity. Recors are compliant with the Scheme request for implementation 9.

### 5.3.1 Structures

There is, in Bigloo, a new class of objects: structures, which are equivalent to C `struct`.

define-struct name field... | bigloo syntax

This form defines a structure with name `name`, which is a symbol, having fields `field`... which are symbols or lists, each list being composed of a symbol and a default value. This form creates several functions: creator, predicate, accessor and assigner functions. The name of each function is built in the following way:

- Creator: `make-`name``
- Predicate: ``name`?`
- Accessor: ``name`-`field``
- Assigner: ``name`-`field`-set!`

Function `make-`name`` accepts an optional argument. If provided, all the slots of the created structures are filled with it. The creator named `name` accepts as many arguments as the number of slots of the structure. This function allocates a structure and fills each of its slots with its corresponding argument.
If a structure is created using `make-`name`` and no initialization value is provided, the slot default values (when provided) are used to initialize the new structure. For instance, the execution of the program:

    (define-struct pt1 a b)
    (define-struct pt2 (h 4) (g 6))

    (make-pt1)
       => #{PT1 () ()}
    (make-pt1 5)
       => #{PT1 5 5}
    (make-pt2)
       => #{PT2 4 6}
    (make-pt2 5)
       => #{PT2 5 5}

struct? obj | bigloo procedure

Returns `#t` if and only if `obj` is a structure.

### 5.3.2 Records (SRFI-9)

Bigloo supports records has specified by SRFI-9. This section is a copy of the SRFI-9 specification by Richard Kelsey. This SRFI describes syntax for creating new data types, called record types. A predicate, constructor, and field accessors and modifiers are defined for each record type. Each new record type is distinct from all existing types, including other record types and Scheme's predefined types.

define-record-type expression... | syntax

The syntax of a record-type definition is:

    <record-type-definition> ==> (define-record-type <type-name>
                                             (<constructor-name> <field-tag> ...)
                                             <predicate-name>
                                             <field-spec> ...)
    <field-spec>             ==> (<field-tag> <accessor-name>)
                               | (<field-tag> <accessor-name> <modifier-name>)
    <field-tag>              ==> <identifier>
    <accessor-name>          ==> <identifier>
    <predicate-name>         ==> <identifier>
    <modifier-name>          ==> <identifier>
    <type-name>              ==> <identifier>

`Define-record-type` is generative: each use creates a new record type that is distinct from all existing types, including other record types and Scheme's predefined types. Record-type definitions may only occur at top-level (there are two possible semantics for `internal' record-type definitions, generative and nongenerative, and no consensus as to which is better).
an instance of `define-record-type` is equivalent to the following definitions:

- `<type-name>` is bound to a representation of the record type itself. Operations on record types, such as defining print methods, reflection, etc. are left to other SRFIs.
- `<constructor-name>` is bound to a procedure that takes as many arguments as the re are `<field-tag>`s in the `(<constructor-name> ...)` subform and returns a new `<type-name>` record. Fields whose tags are listed with `<constructor-name>` have the corresponding argument as their initial value. The initial values of all other fields are unspecified.
- `<predicate-name>` is a predicate that returns `#t` when given a value returned by `<constructor-name>` and `#f` for everything else.
- Each `<accessor-name>` is a procedure that takes a record of type `<type-name>` and returns the current value of the corresponding field. It is an error to pass an accessor a value which is not a record of the appropriate type.
- Each `<modifier-name>` is a procedure that takes a record of type `<type-name>` and a value which becomes the new value of the corresponding field; an unspecified value is returned. It is an error to pass a modifier a first argument which is not a record of the appropriate type.

Records are disjoint from the types listed in Section 4.2 of R5RS.
Seting the value of any of these identifiers has no effect on the behavior of any of their original values.
The following

    (define-record-type pare
        (kons x y)
        pare?
        (x kar set-kar!)
        (y kdr))

defines `kons` to be a constructor, `kar` and `kdr` to be accessors, `set-kar!` to be a modifier, and `pare?` to be a predicate for `pare`s.

      (pare? (kons 1 2))        => #t
      (pare? (cons 1 2))        => #f
      (kar (kons 1 2))          => 1
      (kdr (kons 1 2))          => 2
      (let ((k (kons 1 2)))
        (set-kar! k 3)
        (kar k))                => 3

This Scribe page has been generated by `scribeinfo`.
Last update Tue Dec 16 13:46:41 2003