5.6 Structure Utilities
procedure
(struct->vector v [opaque-v]) → vector?
v : any/c opaque-v : any/c = '...
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
procedure
v : any/c
procedure
v : any/c
procedure
v : any/c
procedure
v : any/c
procedure
(prefab-struct-key v) → (or/c #f symbol? list?)
v : any/c
> (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
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.
> (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)
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
See make-prefab-struct for a description of valid key shapes.
5.6.1 Additional Structure Utilities
(require racket/struct) | package: base |
procedure
(make-constructor-style-printer get-constructor get-contents) → (-> any/c output-port? (or/c #t #f 0 1) void?) get-constructor : (-> any/c (or/c symbol? string?)) get-contents : (-> any/c sequence?)
> (struct point (x y) #:methods gen:custom-write [(define write-proc (make-constructor-style-printer (lambda (obj) 'point) (lambda (obj) (list (point-x obj) (point-y obj)))))])
> (print (point 1 2)) (point 1 2)
> (write (point 1 2)) #<point: 1 2>
The function also cooperates with pretty-print:
> (parameterize ((pretty-print-columns 10)) (pretty-print (point 3000000 4000000)))
(point
3000000
4000000)
> (parameterize ((pretty-print-columns 10)) (pretty-write (point 3000000 4000000)))
#<point:
3000000
4000000>
Added in version 6.3 of package base.
procedure
(struct->list v [#:on-opaque on-opaque]) → (or/c list? #f)
v : any/c on-opaque : (or/c 'error 'return-false 'skip) = 'error
If any fields of v are inaccessible via the current inspector the behavior of struct->list is determined by on-opaque. If on-opaque is 'error (the default), an error is raised. If it is 'return-false, struct->list returns #f. If it is 'skip, the inaccessible fields are omitted from the list.
> (define-struct open (u v) #:transparent)
> (struct->list (make-open 'a 'b)) '(a b)
> (struct->list #s(pre 1 2 3)) '(1 2 3)
> (define-struct (secret open) (x y))
> (struct->list (make-secret 0 1 17 22)) struct->list: expected argument of type <non-opaque struct>;
given: (secret 0 1 ...)
> (struct->list (make-secret 0 1 17 22) #:on-opaque 'return-false) #f
> (struct->list (make-secret 0 1 17 22) #:on-opaque 'skip) '(0 1)
> (struct->list 'not-a-struct #:on-opaque 'return-false) #f
> (struct->list 'not-a-struct #:on-opaque 'skip) '()
Added in version 6.3 of package base.