the constrains the type of an expression inline, and also forms typed exception patterns in catch and handle.
Syntax
;; Expression type annotation
(the ⟨type⟩ ⟨expr⟩)
(the (forall (⟨binder⟩+) ⟨qualified-type⟩) ⟨expr⟩)
;; Typed exception pattern in `catch` or `handle`
((the ⟨exception-type⟩ ⟨var-or-_⟩) ⟨handler-body⟩+)
Semantics
- As an expression:
(the ⟨type⟩ ⟨expr⟩)unifies the inferred type of⟨expr⟩with⟨type⟩and evaluates to⟨expr⟩with no runtime overhead (theis erased after typechecking).⟨type⟩may be a single-value type, a function type,Void(for zero-value expressions), or a multi-value typeT1 * T2 * ...(for multi-value expressions).⟨type⟩may specialize a polymorphic expression (for example, fixing the numeric type of a literal or the container type of[]or[=>]), but it cannot be more general than the inferred type of⟨expr⟩(annotating anIntegerexpression as(the :a expr)is a compile-time error).theannotations without an explicitforallcannot include=>typeclass constraints; wrap constrained annotations in an explicit(forall (⟨binder⟩+) ...)or usedeclareon a binding.⟨type⟩may reference scoped type variables bound by an enclosingforall, or introduce its own explicit(forall (⟨binder⟩+) ...)whose type variables are scoped over⟨expr⟩.- When used directly around the right-hand side of an un-declared recursive binding
(let ((f (the ⟨type⟩ (fn ...)))) ...), the annotation is visible to recursive calls withinf.
- In
catchandhandlebranches:- A branch head written
((the ⟨exception-type⟩ ⟨var-or-_⟩) ⟨handler-body⟩+)is pattern syntax rather than an expression: it matches any thrown exception of⟨exception-type⟩(which must be a concrete exception type with no type variables) and binds the exception value to⟨var-or-_⟩.
- A branch head written
Examples
;; Fix the element and collection type of a builder literal
(coalton (the (Vector F32) [1 2 3 4]))
; => #(1.0 2.0 3.0 4.0)
;; Resolve an ambiguous target type in a conversion
(coalton (+ 1 (the Integer (unwrap (tryInto "41")))))
; => 42
;; Catch any exception of type `BadEgg` and bind the exception value
(coalton-toplevel
(define-exception BadEgg
BrokenEgg
RottenEgg)
(declare inspect-egg ((Void -> Unit) -> (Result BadEgg Unit)))
(define (inspect-egg check)
(catch (Ok (check))
((the BadEgg e) (Err e)))))