On this page:
define-singleton-type
singleton-type?
singleton-type
singleton-type-name
singleton-type-predicate-name
make-singleton-implementation
singleton-descriptor?
initialized-singleton-descriptor?
uninitialized-singleton-descriptor?
singleton-descriptor-predicate
singleton-descriptor-instance
default-singleton-properties
7.4

2.4 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.

Examples:
(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)))
Creates a singleton type and binds the following identifiers:

Examples:
(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

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

  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

(singleton-descriptor? v)  boolean?

  v : any/c

procedure

(initialized-singleton-descriptor? v)  boolean?

  v : any/c

procedure

(uninitialized-singleton-descriptor? v)  boolean?

  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?