On this page:
34.1 Typed macros and procedures
struct-predicate
struct-constructor
struct-accessor
struct-type-is-immutable?
struct-instance-is-immutable?
34.2 Untyped versions of the meta-struct typed macros
struct-predicate
struct-constructor
struct-accessor
struct-type-is-immutable?
struct-instance-is-immutable?
34.3 Untyped for-syntax utilities
meta-struct?
meta-struct-info
get-meta-struct-info
meta-struct-subtype?
struct-type-id-is-immutable?
meta-struct-type-descriptor
!struct-type-descriptor
meta-struct-constructor
!struct-constructor
meta-struct-predicate
!struct-predicate
meta-struct-accessors
!struct-accessors
meta-struct-mutators
!struct-mutators
meta-struct-super-type
!struct-super-type
7.7

34 meta operations on structs

Georges Dupéron <georges.duperon@gmail.com>

34.1 Typed macros and procedures

 (require phc-toolkit/meta-struct) package: phc-toolkit

syntax

(struct-predicate s)

 
s = meta-struct?
Expands to a predicate for the given struct, with the type (-> any/c boolean? : s).

syntax

(struct-constructor s)

 
s = meta-struct?
This macro expands to the constructor function for the given struct, with the type (-> arg s) where each arg corresponds to an argument expected by the struct’s constructor.

syntax

(struct-accessor s i)

(struct-accessor s field)
 
s = meta-struct?
     
i = (expr/c exact-nonnegative-integer?)
     
field = identifier?
This macro expands to the i-th accessor function for the given struct, with the type (-> s t) where t is the struct’s i-th field’s type.

If the second argument is an identifier, then this macro concatenates the identifiers s and field with a - in between, and expands to the resulting s-field. The lexical context of s-field is the same as the lexical context of s. In some rare cases this might not resolve to the accessor for field on s. Passing an exact-nonnegative-integer? as the second argument should be more reliable.

phase 1 procedure

(struct-type-is-immutable? st)  boolean?

  st : Struct-TypeTop
Returns #t if the given struct type can be determined to have only immutable fields. Returns #f otherwise.

procedure

(struct-instance-is-immutable? v)  boolean?

  v : struct?
Returns #t if v can be determined to be an instance of an immutable struct. Returns #f otherwise. Note that when given an instance of an opaque struct struct-instance-is-immutable? cannot access the struct info, and therefore returns #f.

34.2 Untyped versions of the meta-struct typed macros

 (require phc-toolkit/untyped/meta-struct)
  package: phc-toolkit

Untyped version of struct-predicate.
Untyped version of struct-constructor.
Untyped version of struct-accessor.
Untyped version of struct-type-is-immutable?.
Untyped version of struct-instance-is-immutable?.

34.3 Untyped for-syntax utilities

 (require phc-toolkit/untyped/meta-struct)
  package: phc-toolkit

procedure

(meta-struct? v)  boolean?

  v : any/c
Returns #t if v can be used by the functions provided by this module, and #f otherwise. More precisely, v must be an identifier whose syntax-local-value is a struct-info?.

Changed in version 1.0 of package phc-toolkit: This function is provided at phase 1.

struct

(struct meta-struct-info (type-descriptor
    constructor
    predicate
    accessors
    mutators
    super-type)
    #:extra-constructor-name make-meta-struct-info)
  type-descriptor : (or/c identifier? #f)
  constructor : (or/c identifier? #f)
  predicate : (or/c identifier? #f)
  accessors : 
(list*of identifier?
         (or/c (list/c #f) null?))
  mutators : 
(list*of (or/c identifier? #f)
         (or/c (list/c #f) null?))
  super-type : (or/c identifier? #f)
Encapsulates the result of extract-struct-info in a structure with named fields, instead of an obscure six-element list. The precise contents of each field is described in Structure Type Transformer Binding.

Changed in version 1.0 of package phc-toolkit: The identifiers are provided at phase 1.

procedure

(get-meta-struct-info s [#:srcloc srcloc])  meta-struct-info?

  s : meta-struct?
  srcloc : (or/c #f syntax?) = #f
Returns the meta-struct-info for the given identifier. The optional #:srcloc keyword argument gives the source location for error messages in case the given identifier is not a meta-struct?.

Changed in version 1.0 of package phc-toolkit: This function is provided at phase 1.

procedure

(meta-struct-subtype? sub super)  boolean?

  sub : meta-struct?
  super : meta-struct?
Returns #t if the struct associated to the identifier sub is a subtype of the struct associated to the identifier super, and #f otherwise or if the current inspector is not strong enough to know.

Changed in version 1.0 of package phc-toolkit: This function is provided at phase 1.

phase 1 procedure

(struct-type-id-is-immutable? id)  boolean?

  id : identifier?
Returns #t if the struct with the given id can be determined to have only immutable fields. Returns #f otherwise.

procedure

(meta-struct-type-descriptor s 
  [#:srcloc srcloc]) 
  
(expressionof
 ( s (or/c identifier? #f)))
  s : meta-struct?
  srcloc : (or/c #f syntax?) = #f

template metafunction

(!struct-type-descriptor s maybe-srcloc)

 
s = meta-struct?
     
maybe-srcloc = 
  | #:srcloc srcloc
Shorthand for (meta-struct-info-type-descriptor (get-meta-struct-info s)) (with the optional #:srcloc passed to get-meta-struct-info). The precise contents of the returned value field is described in Structure Type Transformer Binding.

Changed in version 1.0 of package phc-toolkit: This function is provided at phase 1.

procedure

(meta-struct-constructor s [#:srcloc srcloc])

  
(expressionof
 ( s (or/c identifier? #f)))
  s : meta-struct?
  srcloc : (or/c #f syntax?) = #f

template metafunction

(!struct-constructor s maybe-srcloc)

 
s = meta-struct?
     
maybe-srcloc = 
  | #:srcloc srcloc
Shorthand for (meta-struct-info-constructor (get-meta-struct-info s)) (with the optional #:srcloc passed to get-meta-struct-info). The precise contents of the returned value field is described in Structure Type Transformer Binding.

Changed in version 1.0 of package phc-toolkit: This function is provided at phase 1.

procedure

(meta-struct-predicate s [#:srcloc srcloc])

  
(expressionof
 ( s (or/c identifier? #f)))
  s : meta-struct?
  srcloc : (or/c #f syntax?) = #f

template metafunction

(!struct-predicate s maybe-srcloc)

 
s = meta-struct?
     
maybe-srcloc = 
  | #:srcloc srcloc
Shorthand for (meta-struct-info-predicate (get-meta-struct-info s)) (with the optional #:srcloc passed to get-meta-struct-info). The precise contents of the returned value field is described in Structure Type Transformer Binding.

Changed in version 1.0 of package phc-toolkit: This function is provided at phase 1.

procedure

(meta-struct-accessors s [#:srcloc srcloc])

  
(expressionof
 ( s (list*of identifier? (or/c (list/c #f) null?))))
  s : meta-struct?
  srcloc : (or/c #f syntax?) = #f

template metafunction

(!struct-accessors s maybe-srcloc)

 
s = meta-struct?
     
maybe-srcloc = 
  | #:srcloc srcloc
Shorthand for (meta-struct-info-accessors (get-meta-struct-info s)) (with the optional #:srcloc passed to get-meta-struct-info). The precise contents of the returned value field is described in Structure Type Transformer Binding.

Changed in version 1.0 of package phc-toolkit: This function is provided at phase 1.

procedure

(meta-struct-mutators s [#:srcloc srcloc])

  
(expressionof
 ( s (list*of (or/c identifier? #f) (or/c (list/c #f) null?))))
  s : meta-struct?
  srcloc : (or/c #f syntax?) = #f

template metafunction

(!struct-mutators s maybe-srcloc)

 
s = meta-struct?
     
maybe-srcloc = 
  | #:srcloc srcloc
Shorthand for (meta-struct-info-mutators (get-meta-struct-info s)) (with the optional #:srcloc passed to get-meta-struct-info). The precise contents of the returned value field is described in Structure Type Transformer Binding.

Changed in version 1.0 of package phc-toolkit: This function is provided at phase 1.

procedure

(meta-struct-super-type s [#:srcloc srcloc])

  
(expressionof
 ( s (or/c identifier? #f)))
  s : meta-struct?
  srcloc : (or/c #f syntax?) = #f

template metafunction

(!struct-super-type s maybe-srcloc)

 
s = meta-struct?
     
maybe-srcloc = 
  | #:srcloc srcloc
Shorthand for (meta-struct-info-super-type (get-meta-struct-info s)) (with the optional #:srcloc passed to get-meta-struct-info). The precise contents of the returned value field is described in Structure Type Transformer Binding.

Changed in version 1.0 of package phc-toolkit: This function is provided at phase 1.