On this page:
Number
Boolean
Symbol
String
Char
S-Exp
Void
->
Listof
Boxof
Vectorof
Parameterof
Hashof
Optionof

5 Types🔗ℹ

syntax

Number

syntax

Boolean

syntax

Symbol

syntax

String

syntax

Char

syntax

S-Exp

syntax

Void

Primitive types.

syntax

(type ... -> type)

Type for functions. Each type before the -> corresponds to a function argument, and the type after -> corresponds to a function result.

syntax

(type * ...+)

Type for tuples. Each *-separated type is the type of an element in the tuple.

syntax

()

Type for the empty tuple.

syntax

(Listof type)

Type for lists of elements, where type is the type of one element.

syntax

(Boxof type)

Type for mutable boxes, where type is the type of the box’s content.

syntax

(Vectorof type)

Type for vectors of elements, where type is the type of one element.

syntax

(Parameterof type)

Type for parameters, where type is the type of the parameter’s value.

syntax

(Hashof type type)

Type for hash tables, where the first type corresponds to keys and the second type correspond to values.

syntax

(Optionof type)

Defined as
(define-type (Optionof 'a)
  (none)
  (some [v : 'a]))
and used, for example, for the result of hash-ref.

syntax

'id

A type variable that stands for an unspecified type. The type checker effectively replaces each type variable with some other type—but possibly another type variable that is bound by an inferred polymorphic type.

In the following example, the type checker determines that 'a must be replaced with Number:

> (define one : 'a 1)
> one

- Number

1

In the following example, the type checker determines that 'b stands for the argument type of a polymorphic function:

> (define (f [x : 'b]) x)

In the following examples, the type checker is unable to replace 'a consistently with the same type everywhere:

> (if (has-type #t : 'a) (has-type 1 : 'a) 2)

eval:240:0: typecheck failed: Number vs. Boolean

  sources:

   1

   #t

   a

> (define x : 'a (list (has-type 1 : 'a)))

eval:241:0: typecheck failed: Number vs. (Listof Number)

  sources:

   (list (has-type 1 : (quote a)))

Multiple uses of the same type variable (i.e., with the same id) are constrained to refer to the same type only when the uses have the same scope. A type variable’s scope is determined as follows:

For example, type variables introduced in separate definitions are always distinct, so in the following example, the first 'a can stand for Number while the second stands for String:

> (define one : 'a 1)
> (define two : 'a "two")

A type variable used for a lambda argument is scoped to the entire lambda form, so the uses of 'a in the definitions of the following example refer back to that argument type, and the argument cannot have both Number and String type:

> (lambda ([x : 'a])
    (local [(define one : 'a 1)
            (define two : 'a "two")]
      #f))

eval:244:0: typecheck failed: Number vs. String

  sources:

   "two"

   1

   a

Beware that the order of expressions can affect the scope of type variables within the expressions:

> (values
    (has-type 1 : 'a)
    (letrec ([f (lambda ([x : 'a]) x)]) f))

- (Number * (Number -> Number))

(values 1 #<procedure:f>)

> (values
    (letrec ([f (lambda ([x : 'a]) x)]) f)
    (has-type 1 : 'a))

- (('_a -> '_a) * Number)

(values #<procedure:f> 1)