4.6 Structure Utilities

► |  | The Racket Reference

1 | Language Model
2 | Syntactic Forms
3 | Datatypes
4 | Structures
5 | Classes and Objects
6 | Units
7 | Contracts
8 | Pattern Matching
9 | Control Flow
10 | Concurrency and Parallelism
11 | Macros
12 | Input and Output
13 | Reflection and Security
14 | Operating System
15 | Memory Management
16 | Unsafe Operations
17 | Running Racket
| Bibliography
| Index

▼ | 4 | Structures

4.1 | Defining Structure Types: struct
4.2 | Creating Structure Types
4.3 | Structure Type Properties
4.4 | Generic Interfaces
4.5 | Copying and Updating Structures
4.6 | Structure Utilities
4.7 | Structure Type Transformer Binding

On this page:

struct->vector
struct?
struct-type?
struct-constructor-procedure?
struct-predicate-procedure?
struct-accessor-procedure?
struct-mutator-procedure?
prefab-struct-key
make-prefab-struct
prefab-key->struct-type
prefab-key?

top ← prev up next →

#### 4.6`` Structure Utilities

> procedure (struct->vector v [opaque-v]) → vector?
> v : any/c
> opaque-v : any/c = '...

Creates a vector representing v. The first slot of the result vector contains a symbol whose printed name has the form struct: id. Each remaining slot contains either the value of a field in v, if it is accessible via the current inspector, or opaque-v for a field that is not accessible. A single opaque-v value is used in the vector for contiguous inaccessible fields. (Consequently, the size of the vector does not match the size of the struct if more than one field is inaccessible.)

> procedure (struct? v) → any
> v : any/c

Returns #t if struct-info exposes any structure types of v with the current inspector, #f otherwise.

Typically, when (struct? v) is true, then (struct->vector v) exposes at least one field value. It is possible, however, for the only visible types of v to contribute zero fields.

> procedure (struct-type? v) → boolean?
> v : any/c

Returns #t if v is a structure type descriptor value, #f otherwise.

> procedure (struct-constructor-procedure? v) → boolean?
> v : any/c

Returns #t if v is a constructor procedure generated by struct or make-struct-type, #f otherwise.

> procedure (struct-predicate-procedure? v) → boolean?
> v : any/c

Returns #t if v is a predicate procedure generated by struct or make-struct-type, #f otherwise.

> procedure (struct-accessor-procedure? v) → boolean?
> v : any/c

Returns #t if v is an accessor procedure generated by struct, make-struct-type, or make-struct-field-accessor, #f otherwise.

> procedure (struct-mutator-procedure? v) → boolean?
> v : any/c

Returns #t if v is a mutator procedure generated by struct, make-struct-type, or make-struct-field-mutator, #f otherwise.

> procedure (prefab-struct-key v) → (or/c #f symbol? list?)
> v : any/c

Returns #f if v is not an instance of a prefab structure type. Otherwise, the result is the shorted key that could be used with make-prefab-struct to create an instance of the structure type.

Examples:
> > (prefab-struct-key #s(cat "Garfield"))
> 'cat
> > (struct cat (name) #:prefab)
> > (struct cute-cat cat (shipping-dest) #:prefab)
> > (cute-cat "Nermel" "Abu Dhabi")
> '#s((cute-cat cat 1) "Nermel" "Abu Dhabi")
> > (prefab-struct-key (cute-cat "Nermel" "Abu Dhabi"))
> '(cute-cat cat 1)

> procedure (make-prefab-struct key v ...) → struct?
> key : prefab-key?
> v : any/c

Creates an instance of a prefab structure type, using the vs as field values. The key and the number of vs determine the prefab structure type.

A key identifies a structure type based on a list with the following items:

- A symbol for the structure type’s name.

- An exact, nonnegative integer representing the number of non-automatic fields in the structure type, not counting fields from the supertype (if any).

- A list of two items, where the first is an exact, nonnegative integer for the number of automatic fields in the structure type that are not from the supertype (if any), and the second element is an arbitrary value that is the value for the automatic fields.

- A vector of exact, nonnegative integers that indicate mutable non-automatic fields in the structure type, counting from 0 and not including fields from the supertype (if any).

- Nothing else, if the structure type has no supertype. Otherwise, the rest of the list is the key for the supertype.

An empty vector and an auto-field list that starts with 0 can be omitted. Furthermore, the first integer (which indicates the number of non-automatic fields) can be omitted, since it can be inferred from the number of supplied vs. Finally, a single symbol can be used instead of a list that contains only a symbol (in the case that the structure type has no supertype, no automatic fields, and no mutable fields).

The total field count must be no more than 32768. If the number of fields indicated by key is inconsistent with the number of supplied vs, the exn:fail:contract exception is raised.

Examples:
> > (make-prefab-struct 'clown "Binky" "pie")
> '#s(clown "Binky" "pie")
> > (make-prefab-struct '(clown 2) "Binky" "pie")
> '#s(clown "Binky" "pie")
> > (make-prefab-struct '(clown 2 (0 #f) #()) "Binky" "pie")
> '#s(clown "Binky" "pie")
> > (make-prefab-struct '(clown 1 (1 #f) #()) "Binky" "pie")
> '#s((clown (1 #f)) "Binky" "pie")
> > (make-prefab-struct '(clown 1 (1 #f) #(0)) "Binky" "pie")
> '#s((clown (1 #f) #(0)) "Binky" "pie")

> procedure (prefab-key->struct-type key field-count) → struct-type?
> key : prefab-key?
> field-count : (integer-in 0 32768)

Returns a structure type descriptor for the prefab structure type specified by the combination of key and field-count.

If the number of fields indicated by key is inconsistent with field-count, the exn:fail:contract exception is raised.

> procedure (prefab-key? v) → boolean?
> v : any/c

Return #t if v can be a prefab structure type key, #f otherwise.

See make-prefab-struct for a description of valid key shapes.

top ← prev up next →