2.5 Singleton Types
(require rebellion/type/singleton) | package: rebellion |
A singleton type is a simple kind of data type made up of only one value. Singleton types are useful for representing named constants, such as the end-of-file marker value eof. Symbols can also be used for this purpose, but a singleton type is often preferable because misspelling the name of the constant causes a compile error rather than a silent bug at runtime.
(define-singleton-type undefined) (define/contract (divide x y) (-> real? real? (or/c real? undefined?)) (if (zero? y) undefined (/ x y)))
> (divide 6 3) 2
> (divide 6 0) #<undefined>
syntax
(define-singleton-type id singleton-option ...)
singleton-option = #:value-name value-id | #:predicate-name predicate-id | #:inspector inspector | #:property-maker property-maker
inspector : inspector?
property-maker :
(-> uninitialized-singleton-descriptor? (listof (cons/c struct-type-property? any/c)))
value-id, which defaults to id —
the unique instance of the created type. predicate-id, which defaults to id? —
a predicate that returns #t when given the singleton instance and false for all other values.
(define-singleton-type infinity)
> infinity #<infinity>
> (infinity? infinity) #t
procedure
(singleton-type? v) → boolean?
v : any/c
procedure
(singleton-type name [ #:predicate-name pred-name]) → singleton-type? name : interned-symbol? pred-name : (or/c interned-symbol? #f) = #f
procedure
(singleton-type-name type) → interned-symbol?
type : singleton-type?
procedure
type : singleton-type?
procedure
(make-singleton-implementation type [ #:inspector inspector #:property-maker prop-maker]) → initialized-singleton-descriptor? type : singleton-type? inspector : inspector? = (current-inspector)
prop-maker :
(-> uninitialized-singleton-descriptor? (listof (cons/c struct-type-property? any/c))) = default-singleton-properties
procedure
v : any/c
procedure
v : any/c
procedure
v : any/c
procedure
(singleton-descriptor-predicate descriptor) → predicate/c
descriptor : singleton-descriptor?
procedure
(singleton-descriptor-instance descriptor)
→ (singleton-descriptor-predicate descriptor) descriptor : initialized-singleton-descriptor?
procedure
(default-singleton-properties descriptor)
→ (listof (cons/c struct-type-property? any/c)) descriptor : singleton-descriptor?