6 Utilities
Typed Racket provides some additional utility functions to facilitate typed programming.
See also the cast form.
> (define: x : (U #f String) (number->string 7)) > x - : (U False String)
"7"
> (assert x) - : String
"7"
> (define: y : (U String Symbol) "hello") > y - : (U String Symbol)
"hello"
> (assert y string?) - : String
"hello"
> (assert y boolean?) Assertion #<procedure:boolean?> failed on "hello"
syntax
(with-asserts ([id maybe-pred] ...) body ...+)
maybe-pred =
| predicate
syntax
(typecheck-fail orig-stx maybe-msg maybe-id)
maybe-msg =
| msg-string maybe-id =
| #:covered-id id
> (define-syntax (cond* stx) (syntax-case stx () [(_ x clause ...) #`(cond clause ... [else (typecheck-fail #,stx "incomplete coverage" #:covered-id x)])]))
> (define: (f [x : (U String Integer)]) : Boolean (cond* x [(string? x) #t] [(exact-nonnegative-integer? x) #f])) eval:10:0: Type Checker: incomplete coverage; missing
coverage of Negative-Integer
in: #f
syntax
(assert-typecheck-fail body-expr)
(assert-typecheck-fail body-expr #:result result-expr)
Added in version 1.7 of package typed-racket-lib.
6.1 Ignoring type information
Added in version 1.7 of package typed-racket-lib.
The expression sub must still type check, but can have any single-valued type.
This is similar to (ann sub Any), but differs in whether the typechecker can use this to refine other types, and can be used in context that do not depend on Typed Racket.
6.2 Untyped Utilities
(require typed/untyped-utils) | package: typed-racket-lib |
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.
Changed in version 1.14 of package typed-racket-lib: The module moved from typed-racket-more to typed-racket-lib.
syntax
(require/untyped-contract maybe-begin module [name subtype] ...)
maybe-begin =
| (begin expr ...)
(require/untyped-contract "my-numerics.rkt" [negate (-> Integer Integer)])
(provide matrix-expt) (require/untyped-contract (begin (require "private/matrix/matrix-types.rkt")) "private/matrix/matrix-expt.rkt" [matrix-expt ((Matrix Number) Integer -> (Matrix Number))])
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.
syntax
(define-typed/untyped-identifier name typed-name untyped-name)
syntax
(define-typed/untyped-identifier name deep-name untyped-name shallow-name optional-name)
(provide my-filter) (define-typed/untyped-identifier my-filter typed:my-filter untyped:my-filter)
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.
procedure
This is the nuclear option, provided because it is sometimes, but rarely, useful. Avoid.