On this page:
top
up

5.2Creating Structure TypesπŸ”— i

procedure

super-type
init-field-cnt
auto-field-cnt
[ auto-v
props
inspector
proc-spec
immutables
guard
constructor-name])
name:symbol?
super-type:(or/c struct-type? #f)
auto-v:any/c =#f
inspector:(or/c inspector? #f'prefab)=(current-inspector )
guard:(or/c procedure? #f)=#f
constructor-name:(or/c symbol? #f)=#f
Creates a new structure type, unless inspector is 'prefab, in which case make-struct-type accesses a prefab structure type. The name argument is used as the type name. If super-type is not #f, the resulting type is a subtype of the corresponding structure type.

The resulting structure type has init-field-cnt+auto-field-cnt fields (in addition to any fields from super-type), but only init-field-cnt constructor arguments (in addition to any constructor arguments from super-type). The remaining fields are initialized with auto-v. The total field count (including super-type fields) must be no more than 32768.

The props argument is a list of pairs, where the car of each pair is a structure type property descriptor, and the cdr is an arbitrary value. A property can be specified multiple times in props (including properties that are automatically added by properties that are directly included in props) only if the associated values are eq? , otherwise the exn:fail:contract exception is raised. See Structure Type Properties for more information about properties. When inspector is 'prefab, then props must be null .

The inspector argument normally controls access to reflective information about the structure type and its instances; see Structure Inspectors for more information. If inspector is 'prefab, then the resulting prefab structure type and its instances are always transparent. If inspector is #f, then the structure type’s instances are transparent.

If proc-spec is an integer or procedure, instances of the structure type act as procedures. See prop:procedure for further information. Providing a non-#f value for proc-spec is the same as pairing the value with prop:procedure at the end of props, plus including proc-spec in immutables when proc-spec is an integer.

The immutables argument provides a list of field positions. Each element in the list must be unique, otherwise exn:fail:contract exception is raised. Each element must also fall in the range 0 (inclusive) to init-field-cnt (exclusive), otherwise exn:fail:contract exception is raised.

The guard argument is either a procedure of n+1 arguments or #f, where n is the number of arguments for the new structure type’s constructor (i.e., init-field-cnt plus constructor arguments implied by super-type, if any). If guard is a procedure, then the procedure is called whenever an instance of the type is constructed, or whenever an instance of a subtype is created. The arguments to guard are the values provided for the structure’s first n fields, followed by the name of the instantiated structure type (which is name, unless a subtype is instantiated). The guard result must be n values, which become the actual values for the structure’s fields. The guard can raise an exception to prevent creation of a structure with the given field values. If a structure subtype has its own guard, the subtype guard is applied first, and the first n values produced by the subtype’s guard procedure become the first n arguments to guard. When inspector is 'prefab, then guard must be #f.

If constructor-name is not #f, it is used as the name of the generated constructor procedure as returned by object-name or in the printed form of the constructor value.

The result of make-struct-type is five values:

Examples:
(define-values (struct:amake-aa?a-refa-set!)
(make-struct-type 'a#f21'uninitialized))
(define an-a(make-a'x'y))
> (a-refan-a1)

'y

> (a-refan-a2)

'uninitialized

> (define a-first(make-struct-field-accessor a-ref0))
> (a-firstan-a)

'x

(define-values (struct:bmake-bb?b-refb-set!)
(make-struct-type 'bstruct:a12'b-uninitialized))
(define a-b(make-b'x'y'z))
> (a-refa-b1)

'y

> (a-refa-b2)

'uninitialized

> (b-refa-b0)

'z

> (b-refa-b1)

'b-uninitialized

> (b-refa-b2)

'b-uninitialized

(define-values (struct:cmake-cc?c-refc-set!)
'cstruct:b00#fnull (make-inspector )#fnull
;guard checks for a number, and makes it inexact
(lambda (a1a2b1name)
(error (string->symbol (format "make-~a"name))
"second field must be a number"))
(values a1(exact->inexact a2)b1))))
> (make-c'x'y'z)

make-c: second field must be a number

> (define a-c(make-c'x2'z))
> (a-refa-c1)

2.0

(define p1#s(pabc))
(define-values (struct:pmake-pp?p-refp-set!)
(make-struct-type 'p#f30#fnull 'prefab#f'(012)))
> (p?p1)

#t

> (p-refp10)

'a

> (make-p'x'y'z)

'#s(p x y z)

procedure

field-pos
[ field/proc-name
arg-contract-str
realm])procedure?
field/proc-name : (or/c symbol? #f)
= (symbol->string (format "field~a"field-pos))
arg-contract-str:(or/c string? symbol? #f)=#f
realm:symbol? ='racket
Returns a field accessor that is equivalent to (lambda (s)(accessor-procsfield-pos)). The accessor-proc must be an accessor returned by make-struct-type .

The field/proc-name argument determines the name of the resulting procedure for error reporting and debugging purposes. If field/proc-name is a symbol and arg-contract-str is not #f, then field/proc-name is used as the procedure name. If field/proc-name is a symbol and arg-contract-str is #f, then field/proc-name is combined with the name of accessor-proc’s structure type to form the procedure name. If field/proc-name is #f, then 'accessor is used as the procedure name.

The arg-contract-str argument determines how the accessor procedure reports an error when it is applied to a value that is not an instance of the accessor-proc’s structure type. If it is a string or symbol, the text of the string or symbol is used as a contract for error reporting. Otherwise, contract text is synthesized from the name of accessor-proc’s structure type.

The realm argument is also used for error reporting. It specifies a realm that an error-message adjuster may use to determine how to adjust an error message. The realm argument also determines the result of procedure-realm for the accessor procedure.

For examples, see make-struct-type .

Changed in version 8.4.0.2 of package base: Added the arg-contract-str and realm arguments.

procedure

field-pos
[ field/proc-name
arg-contract-str
realm])procedure?
field/proc-name : (or/c symbol? #f)
= (symbol->string (format "field~a"field-pos))
arg-contract-str:(or/c string? symbol? #f)=#f
realm:symbol? ='racket
Returns a field mutator that is equivalent to (lambda (sv)(mutator-procsfield-posv)). The mutator-proc must be a mutator returned by make-struct-type .

The field-name, arg-contract-str, and realm arguments are used for error and debugging purposes analogous to the same arguments to make-struct-field-accessor .

For examples, see make-struct-type .

Changed in version 8.4.0.2 of package base: Added the arg-contract-str and realm arguments.

A structure type property that declares a structure type as sealed. The value associated with the property is ignored; the presence of the property itself makes the structure type sealed.

A sealed structure type cannot be used as the supertype of another structure type. Declaring a structure type as sealed is typically just a performance hint, since checking for an instance of a sealed structure type can be slightly faster than checking for an instance of a structure type that might have subtypes.

Added in version 8.0.0.7 of package base.

top
up

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /