On this page:
define-wrapper-type
wrapper-type?
wrapper-type
wrapper-type-name
wrapper-type-predicate-name
wrapper-type-constructor-name
wrapper-type-accessor-name
2.6.1 Wrapper Descriptors
wrapper-descriptor?
initialized-wrapper-descriptor?
uninitialized-wrapper-descriptor?
make-wrapper-implementation
wrapper-descriptor-type
wrapper-descriptor-predicate
wrapper-descriptor-constructor
wrapper-descriptor-accessor
2.6.2 Wrapper Type Properties
default-wrapper-properties
default-wrapper-equal+  hash
default-wrapper-custom-write
7.7

2.6 Wrapper Types

 (require rebellion/type/wrapper) package: rebellion

A wrapper type is a kind of data type for values that are simple wrappers around other values. An instance of a wrapper type has a single field containing the wrapped value, and two instances of the same wrapper type are equal? if they wrap equal? values. Wrapper types are useful when the same kind of data is used in many different ways that need to be distinguished.

(define-wrapper-type celsius)
(define-wrapper-type fahrenheit)
 
(define/contract (celsius->fahrenheit c)
  (-> celsius? fahrenheit?)
  (fahrenheit (+ (* (celsius-value c) 9/5) 32)))

 

> (celsius->fahrenheit (celsius 0))

(fahrenheit 32)

> (celsius->fahrenheit (celsius 100))

(fahrenheit 212)

> (celsius->fahrenheit (fahrenheit 100))

celsius->fahrenheit: contract violation

  expected: celsius?

  given: (fahrenheit 100)

  in: the 1st argument of

      (-> celsius? fahrenheit?)

  contract from:

      (function celsius->fahrenheit)

  blaming: top-level

   (assuming the contract is correct)

  at: eval:5.0

syntax

(define-wrapper-type id option ...)

 
option = #:omit-root-binding
  | #:constructor-name constructor-id
  | #:accessor-name accessor-id
  | #:predicate-name predicate-id
  | #:pattern-name pattern-id
  | #:property-maker prop-maker-expr
 
  prop-maker-expr : 
(-> uninitialized-wrapper-descriptor?
    (listof (cons/c struct-type-property? any/c)))
Creates a new wrapper type named id and binds the following identifiers:

Additionally, unless #:omit-root-binding is specified, the original id is bound to pattern-id when used in match patterns and to constructor-id when used as an expression. Use #:omit-root-binding when you want control over what id is bound to, such as when creating a smart constructor.

Examples:
> (define-wrapper-type seconds)
> (seconds 10)

(seconds 10)

> (seconds-value (seconds 25))

25

> (seconds? (seconds 10))

#t

> (match-define (seconds (? even? x)) (seconds 10))
> (match-define (seconds (? even? x)) (seconds 25))

match-define: no matching clause for (seconds 25)

procedure

(wrapper-type? v)  boolean?

  v : any/c

procedure

(wrapper-type name 
  [#:predicate-name predicate-name 
  #:constructor-name constructor-name 
  #:accessor-name accessor-name]) 
  wrapper-type?
  name : interned-symbol?
  predicate-name : (or/c interned-symbol? #f) = #f
  constructor-name : (or/c interned-symbol? #f) = #f
  accessor-name : (or/c interned-symbol? #f) = #f

procedure

(wrapper-type-name type)  interned-symbol?

  type : wrapper-type?

procedure

(wrapper-type-predicate-name type)  interned-symbol?

  type : wrapper-type?

procedure

(wrapper-type-constructor-name type)  interned-symbol?

  type : wrapper-type?

procedure

(wrapper-type-accessor-name type)  interned-symbol?

  type : wrapper-type?
2.6.1 Wrapper Descriptors

procedure

(wrapper-descriptor? v)  boolean?

  v : any/c

procedure

(initialized-wrapper-descriptor? v)  boolean?

  v : any/c

procedure

(uninitialized-wrapper-descriptor? v)  boolean?

  v : any/c

procedure

(make-wrapper-implementation type 
  [#:property-maker prop-maker 
  #:inspector inspector]) 
  initialized-wrapper-descriptor?
  type : wrapper-type?
  prop-maker : 
(-> uninitialized-wrapper-descriptor?
    (listof (cons/c struct-type-property? any/c)))
   = default-wrapper-properties
  inspector : inspector? = (current-inspector)

procedure

(wrapper-descriptor-type descriptor)  wrapper-type?

  descriptor : wrapper-descriptor?

procedure

(wrapper-descriptor-predicate descriptor)  predicate/c

  descriptor : wrapper-descriptor?

procedure

(wrapper-descriptor-constructor descriptor)

  (-> any/c (wrapper-descriptor-predicate descriptor))
  descriptor : wrapper-descriptor?

procedure

(wrapper-descriptor-accessor descriptor)

  (-> (wrapper-descriptor-predicate descriptor) any/c)
  descriptor : wrapper-descriptor?
2.6.2 Wrapper Type Properties

procedure

(default-wrapper-properties descriptor)

  (listof (cons/c struct-type-property? any/c))
  descriptor : wrapper-descriptor?

procedure

(default-wrapper-equal+hash descriptor)  equal+hash/c

  descriptor : wrapper-descriptor?

procedure

(default-wrapper-custom-write descriptor)

  custom-write-function/c
  descriptor : wrapper-descriptor?