On this page:
assert
with-asserts
defined?
index?
Version: 5.2

4 Utilities

Typed Racket provides some additional utility functions to facilitate typed programming.

(assert v)  A
  v : (U #f A)
(assert v p?)  B
  v : A
  p? : (A -> Any : B)
Verifies that the argument satisfies the constraint. If no predicate is provided, simply checks that the value is not #f.

Examples:

> (define: x : (U #f String) (number->string 7))
> x

- : (U String False)

"7"

> (assert x)

- : String

"7"

> (define: y : (U String Symbol) "hello")
> y

- : (U Symbol String)

"hello"

> (assert y string?)

- : String

"hello"

> (assert y boolean?)

Assertion failed

(with-asserts ([id maybe-pred] ...) body ...+)
 
maybe-pred = 
  | predicate
Guard the body with assertions. If any of the assertions fail, the program errors. These assertions behave like assert.

(defined? v)  boolean?
  v : any/c
A predicate for determining if v is not #<undefined>.

(index? v)  boolean?
  v : any/c
A predicate for the Index type.