6.2.900.17
6 Utilities
Link to this section with
@secref["Utilities"
#:doc '(lib "typed-racket/scribblings/ts-reference.scrbl")]
Link to this section with
@secref["Utilities"
#:doc '(lib "typed-racket/scribblings/ts-reference.scrbl")]
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
#f.
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
type.
(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.
Examples: |
| | | eval:10:0: Type Checker: incomplete coverage; missing | coverage of Negative-Integer | in: #f |
|
6.1 Untyped Utilities
Link to this section with
@secref["Untyped_Utilities"
#:doc '(lib "typed-racket/scribblings/ts-reference.scrbl")]
Link to this section with
@secref["Untyped_Utilities"
#:doc '(lib "typed-racket/scribblings/ts-reference.scrbl")]
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 is
"my-numerics.rkt",
it can be imported and used in untyped code this way:
The
require/untyped-contract form expands into a submodule
with language
typed/racket/base. Identifiers used in
subtype expressions must be either in Typed Racket’s base type
environment (e.g.
Integer and
Listof) or defined by an
expression in the
maybe-begin form, which is spliced into the
submodule. For example, the
math/matrix module imports and
reexports
matrix-expt, which has a
case-> type,
for untyped use in this way:
The
(require "private/matrix/matrix-types.rkt") expression imports the
Matrix type.
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
an identifier.
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
where
typed:my-filter is the original
my-filter, but imported
using
prefix-in, 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
#f.
This is the nuclear option, provided because it is sometimes, but rarely, useful.
Avoid.