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)))
constructor-id, which defaults to constructor:id —
a constructor function that wraps a value and returns an instance of the created type. predicate-id, which defaults to id? —
a predicate function that returns #t when given instances of the created type and returns #f otherwise. accessor-id, which defaults to id-value —
an accessor function that unwraps instances of the created type and returns their underlying value. pattern-id, which defaults to pattern:id —
a match expander that unwraps instances of the created type and matches their contents against a subpattern.
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.
> (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
type : wrapper-type?
procedure
type : wrapper-type?
procedure
type : wrapper-type?
2.6.1 Wrapper Descriptors
procedure
(wrapper-descriptor? v) → boolean?
v : any/c
procedure
v : any/c
procedure
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?