Typed Racket provides some additional utility functions to facilitate typed programming.
Verifies that the argument satisfies the constraint. If no predicate
is provided, simply checks that the value is not
See also the cast form.
Guard the body with assertions. If any of the assertions fail, the
program errors. These assertions behave like assert
A predicate for the Index
(typecheck-fail orig-stx maybe-msg maybe-id)
|maybe-msg|| ||=|| |
| || ||||| ||msg-string|
| || || || || |
|maybe-id|| ||=|| |
| || ||||| ||#:covered-id id|
Explicitly produce a type error, with the source location or
orig-stx. If msg-string is present, it must be a literal string, it is used as
the error message, otherwise the error message
"Incomplete case coverage" is used.
If id is present and has
type T, then the message "missing coverage of T" is added to
the error message.
Type Checker: type mismatch
(List (List 'string? 'x) True)
(List (List 'exact-nonnegative-integer? 'x)
5.1 Untyped Utilities
These utilities help interface typed with untyped code, particularly typed
libraries that use types that cannot be converted into contracts, or export
syntax transformers that must expand differently in typed and untyped contexts.
Use this form to import typed identifiers whose types cannot be converted into
contracts, but have subtypes that can be converted into contracts.
For example, suppose we define and provide the Typed Racket function
Trying to use negate
within an untyped module will raise an error
because the cases cannot be distinguished by arity alone.
If the defining module for negate
it can be imported and used in untyped code this way:
form expands into a submodule
with language typed/racket/base
. Identifiers used in
expressions must be either in Typed Racket’s base type
environment (e.g. Integer
) or defined by an
expression in the maybe-begin
form, which is spliced into the
submodule. For example, the math/matrix
module imports and
, which has a case->
for untyped use in this way:
The (require "private/matrix/matrix-types.rkt")
expression imports the
If an identifier name is imported using require/untyped-contract,
reexported, and imported into typed code, it has its original type, not
subtype. In other words, subtype is used only to generate
a contract for name, not to narrow its type.
Because of limitations in the macro expander, require/untyped-contract
cannot currently be used in typed code.
Defines an identifier name that expands to typed-name in typed
contexts and to untyped-name in untyped contexts. Each subform must be
Suppose we define and provide a Typed Racket function with this type:
In this case, we might still provide my-filter to untyped code using
is the original my-filter
, but imported
, and untyped:my-filter
is either a Typed Racket
implementation of it with type (All (a) (-> (-> Any Any) (Listof Any) (Listof a)))
or an untyped Racket implementation.
Avoid this if possible. Use only in cases where a type has no subtype that can
be converted to a contract; i.e. cases in which require/untyped-contract
cannot be used.
Returns #t if called while expanding code in a typed context; otherwise
This is the nuclear option, provided because it is sometimes, but rarely, useful.