2 Special Form Reference
The Typed Racket Reference
Type Reference
Special Form Reference
Libraries Provided With Typed Racket
Typed Classes
Typed Units
Utilities
Exploring Types
Deep, Shallow, and Optional Semantics
Typed Racket Syntax Without Type Checking
10
Typed Regions
11
Optimization in Typed Racket
12
Unsafe Typed Racket operations
13
Legacy Forms
14
Compatibility Languages
15
Experimental Features
Bibliography
Index
Special Form Reference
2.1
Binding Forms
2.2
Anonymous Functions
2.3
Loops
2.4
Definitions
2.5
Structure Definitions
2.6
Names for Types
2.7
Generating Predicates Automatically
2.8
Type Annotation and Instantiation
2.9
Require
2.10
Other Forms
2.11
Special Structure Type Properties
On this page:
2.1
Binding Forms
let
letrec
let*
let-
values
letrec-
values
let*-
values
let/
cc
let/
ec
2.2
Anonymous Functions
lambda
case-
lambda
case-
2.3
Loops
for
for/
list
for/
hash
for/
hasheq
for/
hasheqv
for/
hashalw
for/
vector
for/
or
for/
sum
for/
product
for/
last
for/
set
for*/
list
for*/
hash
for*/
hasheq
for*/
hasheqv
for*/
hashalw
for*/
vector
for*/
or
for*/
sum
for*/
product
for*/
last
for*/
set
for/
and
for/
first
for*/
and
for*/
first
for/
lists
for/
fold
for/
foldr
for*
for*/
lists
for*/
fold
for*/
foldr
do
2.4
Definitions
define
2.5
Structure Definitions
struct
define-
struct
2.6
Names for Types
define-
type
2.7
Generating Predicates Automatically
make-
predicate
define-
predicate
2.8
Type Annotation and Instantiation
provide:
ann
cast
inst
row-
inst
2.9
Require
require/
typed
require/
typed/
provide
2.10
Other Forms
with-
handlers
with-
handlers*
default-
continuation-
prompt-
tag
#%module-
begin
#%top-
interaction
2.11
Special Structure Type Properties
prop:
procedure
9.1
Racket
top
contents
← prev
up
next →
Special Form Reference
Typed Racket provides a variety of special forms above and beyond
those in Racket. They are used for annotating variables with types,
creating new types, and annotating expressions.
2.1
Binding Forms
loop
, and
var
are names,
type
is a type.
is an expression and
body
is a block.
syntax
let
maybe-tvars
binding
...
maybe-ret
body
let
loop
maybe-ret
binding
...
body
binding
var
var
type
maybe-tvars
#:forall
tvar
...
#:∀
tvar
...
maybe-ret
type0
Local bindings, like
let
, each with associated types.
In the first form,
maybe-ret
can only appear with
maybe-tvars
so if you only want to specify the return type,
you should set
maybe-tvars
to
#:forall
Examples:
let
Zero
- : Integer [more precisely: Zero]
let
#:forall
Zero
Natural
- : Integer [more precisely: Zero]
let
Zero
Natural
eval:4:0: :: bad syntax
in: :
If polymorphic type variables are provided, they are bound in the type
expressions for variable bindings.
Example:
let
#:forall
- : Integer [more precisely: Zero]
In the second form,
type0
is the type of the
result of
loop
(and thus the result of the entire
expression as well as the final expression in
body
).
Type annotations are optional.
Examples:
filter-even
->
Listof
Natural
Listof
Natural
Listof
Natural
define
filter-even
lst
accum
if
null?
lst
accum
let
first
Natural
car
lst
rest
Listof
Natural
cdr
lst
if
even?
first
filter-even
rest
cons
first
accum
filter-even
rest
accum
filter-even
list
null
- : (Listof Nonnegative-Integer)
'(6 4 2)
Examples:
filter-even-loop
->
Listof
Natural
Listof
Natural
define
filter-even-loop
lst
let
loop
Listof
Natural
accum
Listof
Natural
null
lst
Listof
Natural
lst
cond
null?
lst
accum
even?
car
lst
loop
cons
car
lst
accum
cdr
lst
else
loop
accum
cdr
lst
filter-even-loop
list
- : (Listof Nonnegative-Integer)
'(4 2)
syntax
letrec
binding
...
body
syntax
let*
binding
...
body
syntax
let-values
var+type
...
...
body
syntax
letrec-values
var+type
...
...
body
syntax
let*-values
var+type
...
...
body
Type-annotated versions of
letrec
let*
let-values
letrec-values
, and
let*-values
. As with
let
, type annotations are optional.
syntax
let/cc
body
syntax
let/ec
body
Type-annotated versions of
let/cc
and
let/ec
. As with
let
, the type annotation is optional.
2.2
Anonymous Functions
syntax
lambda
maybe-tvars
formals
maybe-ret
body
formals
formal
...
formal
...
rst
formal
var
var
default-expr
var
type
var
type
default-expr
keyword
var
keyword
var
type
keyword
var
type
default-expr
rst
var
var
type
var
type
ooo
bound
maybe-tvars
#:forall
tvar
...
#:∀
tvar
...
#:forall
tvar
...
ooo
#:∀
tvar
...
ooo
maybe-ret
type
Constructs an anonymous function like the
lambda
form from
racket/base
, but allows type annotations on the formal
arguments. If a type annotation is left out, the formal will have
the type
Any
Examples:
lambda
String
string-append
"bar"
- : (-> String String)
#
lambda
Integer
add1
- : (-> Any Integer Integer)
#
lambda
- : (-> Any Any)
#
Type annotations may also be specified for keyword and optional arguments:
Examples:
lambda
String
"foo"
string-append
"bar"
- : (->* () (String) (String : (Top | Bot)))
#
lambda
#:x
String
string-append
"bar"
- : (-> #:x String String)
#
lambda
#:y
Integer
add1
- : (-> Any [#:y Integer] Integer)
#
lambda
default
- : (->* () (Any) Any)
#
The
lambda
expression may also specify polymorphic type variables
that are bound for the type expressions in the formals.
Examples:
lambda
#:forall
- : (All (A) (-> A A))
#
lambda
#:∀
- : (All (A) (-> A A))
#
In addition, a type may optionally be specified for the
rest argument
with either a uniform type or using a polymorphic type. In the former case,
the rest argument is given the type
Listof
type
where
type
is the provided type annotation.
Examples:
lambda
rst
rst
- : (-> Any Any * (Listof Any))
#
lambda
rst
Integer
rst
- : (-> Any Integer * (Listof Integer))
#
lambda
#:forall
...
rst
...
rst
- : (All (A ...) (-> Any A ... A (List A ... A)))
#
syntax
An alias for
lambda
syntax
case-lambda
maybe-tvars
formals
body
...
A function of multiple arities. The
formals
are identical
to those accepted by the
lambda
form except that keyword
and optional arguments are not allowed.
Polymorphic type variables, if provided, are bound in the type
expressions in the formals.
Note that each
formals
must have a different arity.
Example:
define
add-map
case-lambda
lst
Listof
Integer
map
add1
lst
lst1
Listof
Integer
lst2
Listof
Integer
map
lst1
lst2
To see how to declare a type for
add-map
, see the
case->
type constructor.
syntax
case-λ
An alias for
case-lambda
2.3
Loops
syntax
for
void-ann-maybe
for-clause
...
void-ann-maybe
expr
...+
void-ann-maybe
Void
type-ann-maybe
for-clause
id
seq-expr
binding
...
seq-expr
id
seq-expr
#:when
guard
#:unless
guard
#:do
do-body
...
break-clause
#:splice
splicing-id
form
binding
id
id
break-clause
#:break
guard
#:final
guard
Like
for
from
racket/base
, but each
id
has the associated type
. The latter
ann-maybe
will be used first, and then the previous one.
Since the return type is always
Void
annotating the return type of a
for
form is optional.
Type annotations in clauses are optional for all
for
variants.
Examples:
for
for
Void
for
Void
for
Void
Void
for
Any
eval:29:0: :: bad syntax
in: :
for/or
False
False
#f
- : False
#f
for/or
Boolean
False
#f
- : Boolean
#f
for/or
False
Boolean
#f
eval:32:0: Type Checker: type mismatch
expected: False
given: Boolean
in: #f
syntax
for/list
type-ann-maybe
for-clause
...
type-ann-maybe
expr
...+
syntax
for/hash
type-ann-maybe
for-clause
...
type-ann-maybe
expr
...+
syntax
for/hasheq
type-ann-maybe
for-clause
...
type-ann-maybe
expr
...+
syntax
for/hasheqv
type-ann-maybe
for-clause
...
type-ann-maybe
expr
...+
syntax
for/hashalw
type-ann-maybe
for-clause
...
type-ann-maybe
expr
...+
syntax
for/vector
type-ann-maybe
for-clause
...
type-ann-maybe
expr
...+
syntax
for/or
type-ann-maybe
for-clause
...
type-ann-maybe
expr
...+
syntax
for/sum
type-ann-maybe
for-clause
...
type-ann-maybe
expr
...+
syntax
for/product
type-ann-maybe
for-clause
...
type-ann-maybe
expr
...+
syntax
for/last
type-ann-maybe
for-clause
...
type-ann-maybe
expr
...+
syntax
for/set
type-ann-maybe
for-clause
...
type-ann-maybe
expr
...+
syntax
for*/list
type-ann-maybe
for-clause
...
type-ann-maybe
expr
...+
syntax
for*/hash
type-ann-maybe
for-clause
...
type-ann-maybe
expr
...+
syntax
for*/hasheq
type-ann-maybe
for-clause
...
type-ann-maybe
expr
...+
syntax
for*/hasheqv
type-ann-maybe
for-clause
...
type-ann-maybe
expr
...+
syntax
for*/hashalw
type-ann-maybe
for-clause
...
type-ann-maybe
expr
...+
syntax
for*/vector
type-ann-maybe
for-clause
...
type-ann-maybe
expr
...+
syntax
for*/or
type-ann-maybe
for-clause
...
type-ann-maybe
expr
...+
syntax
for*/sum
type-ann-maybe
for-clause
...
type-ann-maybe
expr
...+
syntax
for*/product
type-ann-maybe
for-clause
...
type-ann-maybe
expr
...+
syntax
for*/last
type-ann-maybe
for-clause
...
type-ann-maybe
expr
...+
syntax
for*/set
type-ann-maybe
for-clause
...
type-ann-maybe
expr
...+
These behave like their non-annotated counterparts, with the exception
that
#:when
clauses can only appear as the last
for-clause
. The return value of the entire form must be of
type
. For example, a
for/list
form would be
annotated with a
Listof
type. All annotations are optional.
syntax
for/and
type-ann-maybe
for-clause
...
type-ann-maybe
expr
...+
syntax
for/first
type-ann-maybe
for-clause
...
type-ann-maybe
expr
...+
syntax
for*/and
type-ann-maybe
for-clause
...
type-ann-maybe
expr
...+
syntax
for*/first
type-ann-maybe
for-clause
...
type-ann-maybe
expr
...+
Like the above, except they are not yet supported by the typechecker.
syntax
for/lists
type-ann-maybe
id
...
maybe-result
for-clause
...
type-ann-maybe
expr
...+
syntax
for/fold
type-ann-maybe
id
init-expr
...
maybe-result
for-clause
...
type-ann-maybe
expr
...+
syntax
for/foldr
type-ann-maybe
id
init-expr
...
maybe-result
for-clause
...
type-ann-maybe
expr
...+
maybe-result
#:result
result-expr
These behave like their non-annotated counterparts. Unlike the above,
#:when
clauses can be used freely with these.
Changed in version 1.11 of package
typed-racket-lib
: Added the
#:result
form.
Changed in version 1.12 of package
typed-racket-lib
: Added
for/foldr
syntax
for*
void-ann-maybe
for-clause
...
void-ann-maybe
expr
...+
syntax
for*/lists
type-ann-maybe
id
...
maybe-result
for-clause
...
type-ann-maybe
expr
...+
syntax
for*/fold
type-ann-maybe
id
init-expr
...
maybe-result
for-clause
...
type-ann-maybe
expr
...+
syntax
for*/foldr
type-ann-maybe
id
init-expr
...
maybe-result
for-clause
...
type-ann-maybe
expr
...+
maybe-result
#:result
result-expr
These behave like their non-annotated counterparts.
Changed in version 1.11 of package
typed-racket-lib
: Added the
#:result
form.
Changed in version 1.12 of package
typed-racket-lib
: Added
for*/foldr
syntax
do
id
init-expr
step-expr-maybe
...
stop?-expr
finish-expr
...
expr
...+
step-expr-maybe
step-expr
Like
do
from
racket/base
, but each
id
having the associated type
, and
the final body
expr
having the type
. Type
annotations are optional.
2.4
Definitions
syntax
define
maybe-tvars
maybe-ann
define
maybe-tvars
header
maybe-ann
body
header
function-name
formals
header
formals
formals
formal
...
formal
...
rst
formal
var
var
default-expr
var
type
var
type
default-expr
keyword
var
keyword
var
type
keyword
var
type
default-expr
rst
var
var
type
var
type
ooo
bound
maybe-tvars
#:forall
tvar
...
#:∀
tvar
...
#:forall
tvar
...
ooo
#:∀
tvar
...
ooo
maybe-ann
type
Like
define
from
racket/base
, but allows optional
type annotations for the variables.
The first form defines a variable
to the result of evaluating
the expression
. The variable may have an optional type annotation.
Examples:
define
foo
"foo"
define
bar
Integer
10
If polymorphic type variables are provided, then they are bound for use
in the type annotation.
Example:
define
#:forall
mt-seq
Sequenceof
empty-sequence
The second form allows the definition of functions with optional
type annotations on any variables. If a return type annotation is
provided, it is used to check the result of the function.
Like
lambda
, optional and keyword arguments are supported.
Examples:
define
add
first
Integer
second
Integer
Integer
first
second
define
#:forall
poly-app
func
->
first
second
func
first
second
The function definition form also allows curried function arguments with
corresponding type annotations.
Examples:
define
addx
Number
Number
define
add2
addx
add2
- : Number
Note that unlike
define
from
racket/base
define
does not bind functions with keyword arguments to static information about
those functions.
2.5
Structure Definitions
syntax
struct
maybe-type-vars
name-spec
...
options
...
maybe-type-vars
...
name-spec
name-id
name-id
parent
options
#:transparent
#:mutable
#:prefab
#:constructor-name
constructor-id
#:extra-constructor-name
constructor-id
#:property
property-id
property-expr
#:type-name
type-id
Defines a
structure
with the name
name-id
, where the
fields
have types
, similar to the behavior of
struct
from
racket/base
Examples:
struct
camelia-sinensis
age
Integer
struct
camelia-sinensis-assamica
camelia-sinensis
If
type-id
is not specified,
name-id
will be used for the
name of the type associated with instances of the declared
structure. Otherwise,
type-id
will be used for the type name, and
using
name-id
in this case will cause a type error.
Examples:
struct
apple
#:type-name
BigApple
ann
apple
BigApple
- : BigApple
#
ann
apple
apple
eval:45:0: Type Checker: parse error in type;
type name `apple' is unbound
in: apple
type-id
can be also used as an alias to
name-id
, i.e. it will
be a transformer binding that encapsulates the same structure information as
name-id
does.
Examples:
struct
avocado
amount
Integer
#:type-name
Avocado
struct
hass-avocado
Avocado
struct-copy
Avocado
avocado
amount
42
- : Avocado
#
When
parent
is present, the
structure is a substructure of
parent
When
maybe-type-vars
is present, the structure is polymorphic in the type
variables
. If
parent
is also a polymorphic struct, then
there must be at least as many type variables as in the parent type, and the
parent type is instantiated with a prefix of the type variables matching the
amount it needs.
Examples:
struct
2-tuple
first
second
struct
3-tuple
2-tuple
third
Options provided have the same meaning as for the
struct
form
from
racket/base
(with the exception of
#:type-name
, as
described above).
A prefab structure type declaration will bind the given
name-id
or
type-id
to a
Prefab
type. Unlike the
struct
form from
racket/base
, a non-prefab structure type cannot extend
a prefab structure type.
Examples:
struct
a-prefab
String
#:prefab
:type
a-prefab
(Prefab a-prefab String)
struct
not-allowed
a-prefab
eval:53:0: Type Checker: Error in macro expansion -- parent
type not a valid structure name: a-prefab
in: ()
Changed in version 1.4 of package
typed-racket-lib
: Added the
#:type-name
option.
syntax
define-struct
maybe-type-vars
name-spec
...
options
...
maybe-type-vars
...
name-spec
name-id
name-id
parent
options
#:transparent
#:mutable
#:type-name
type-id
Legacy version of
struct
, corresponding to
define-struct
from
racket/base
Changed in version 1.4 of package
typed-racket-lib
: Added the
#:type-name
option.
2.6
Names for Types
syntax
define-type
name
maybe-omit-def
define-type
name
...
maybe-omit-def
maybe-omit-def
#:omit-define-syntaxes
The first form defines
name
as type, with the same meaning as
. The second form defines
name
to be a type
constructor, whose parameters are
...
and body is
. If no parameters are declared, the defined type
constructor is equivalent to
define-type
name
maybe-omit-def
. Type names may refer to other types defined in the
same or enclosing scopes.
Examples:
define-type
IntStr
Integer
String
define-type
ListofPairs
Listof
Pair
If
#:omit-define-syntaxes
is specified, no definition of
name
is created. In this case, some other definition of
name
is necessary.
If the body of the type definition refers to itself, then the
type definition is recursive. Recursion may also occur mutually,
if a type refers to a chain of other types that eventually refers
back to itself.
Examples:
define-type
BT
Number
Pair
BT
BT
let
define-type
Even
Null
Pairof
Odd
define-type
Odd
Pairof
Even
even-lst
Even
Integer
define
even-lst
even-lst
- : (Even Integer)
'(1 2)
However, the recursive reference is only allowed when it is passed to a productive
type constructor:
Examples:
define-type
Foo
Foo
eval:58:0: Type Checker: Error in macro expansion -- parse
error in type;
not in a productive position
variable: Foo
in: Foo
define-type
Bar
Bar
False
eval:59:0: Type Checker: Error in macro expansion -- parse
error in type;
not in a productive position
variable: Bar
in: False
define-type
Bar
Listof
Bar
False
2.7
Generating Predicates Automatically
syntax
make-predicate
Evaluates to a predicate for the type
, with the type
Any
->
Boolean
may not contain function types, or
types that may refer to mutable data such as
Vectorof
Integer
syntax
define-predicate
name
Equivalent to
define
name
make-predicate
2.8
Type Annotation and Instantiation
syntax
This declares that
has type
The definition of
must appear after this declaration. This
can be used anywhere a definition form may be used.
Examples:
var1
Integer
var2
String
The second form allows type annotations to elide one level of parentheses
for function types.
Examples:
var3
->
Integer
var4
String
->
Integer
syntax
provide:
...
This declares that the
s have
the types
, and also provides all of the
s.
syntax
This declares that the variable
has type
. This is legal only for binding occurrences of
If a dispatch macro on
#\{
already exists in the current
readtable
, this
syntax will be disabled.
syntax
ann
Ensure that
has type
, or
some subtype. The entire expression has type
This is legal only in expression contexts.
syntax
::
A reader abbreviation for
ann
If a dispatch macro on
#\{
already exists in the current
readtable
, this
syntax will be disabled.
syntax
cast
The entire expression has the type
, while
may have any type. The value of the entire expression is the value
returned by
, protected by a contract ensuring that it has type
. This is legal only in expression contexts.
Examples:
cast
Integer
- : Integer
cast
String
(cast for #f): broke its own contract
promised: string?
produced: 3
in: string?
contract from: cast
blaming: cast
(assuming the contract is correct)
at: eval:66:0
cast
lambda
Any
String
->
String
- : (-> String String)
#
cast
lambda
Any
String
->
String
"hello"
- : String
"hello"
The value is actually protected with two contracts. The second contract checks
the new type, but the first contract is put there to enforce the old type, to
protect higher-order uses of the value.
Examples:
cast
lambda
String
Any
->
Any
"hello"
- : Any
"hello"
cast
lambda
String
Any
->
Any
(cast for val): contract violation
expected: string?
given: 5
in: the 1st argument of
(-> string? any)
contract from: typed-world
blaming: cast
(assuming the contract is correct)
at: eval:70:0
cast
will wrap the value
in a contract which will affect
the runtime performance of reading and updating the value. This is needed when
is a complex data type, such as a hash table. However, when the type
of the value can be checked using a simple predicate, consider using
assert
instead.
syntax
inst
...
inst
...
ooo
bound
Instantiate the type of
with types
...
or with the
poly-dotted types
...
ooo
bound
must
have a polymorphic type that can be applied to the supplied number of type
variables. For non-poly-dotted functions, however, fewer arguments can be
provided and the omitted types default to
Any
inst
is legal only in expression contexts.
Examples:
foldl
inst
cons
Integer
Integer
null
list
- : (Listof Integer)
'(4 3 2 1)
my-cons
All
->
Pairof
define
my-cons
cons
foldl-list
All
Listof
->
Listof
define
foldl-list
lst
foldl
inst
my-cons
Listof
null
lst
foldl-list
list
"1"
"2"
"3"
"4"
- : (Listof String)
'("4" "3" "2" "1")
foldr-list
All
Listof
->
Any
define
foldr-list
lst
foldr
inst
my-cons
null
lst
foldr-list
list
"1"
"2"
"3"
"4"
- : Any
'("1" "2" "3" "4")
my-values
All
...
...
->
values
...
define
my-values
arg
args
apply
inst
values
...
arg
args
syntax
row-inst
row
Instantiate the row-polymorphic type of
with
row
. This is legal only in expression contexts.
Examples:
id
All
#:row
->
Class
#:row-var
Class
#:row-var
define
id
cls
cls
row-inst
id
Row
field
Integer
class
object%
super-new
field
Integer
- : (Class (field (x Integer)))
#
syntax
...
A reader abbreviation for
inst
...
syntax
...
ooo
bound
A reader abbreviation for
inst
...
ooo
bound
2.9
Require
Here,
is a module spec,
pred
is an identifier
naming a predicate, and
maybe-renamed
is an
optionally-renamed identifier.
syntax
require/typed
rt-clause
...
rt-clause
maybe-renamed
#:struct
maybe-tvars
name-id
...
struct-option
...
#:struct
maybe-tvars
name-id
parent
...
struct-option
...
#:opaque
pred
#:signature
name
id
...
maybe-renamed
id
orig-id
new-id
maybe-tvars
type-variable
...
struct-option
#:constructor-name
constructor-id
#:extra-constructor-name
constructor-id
#:type-name
type-id
This form requires identifiers from the module
, giving
them the specified types.
The first case requires
maybe-renamed
, giving it type
The second and third cases require the struct with name
name-id
and creates a new type with the name
type-id
, or
name-id
if no
type-id
is provided, with fields
...
where each field has type
. The third case allows a
parent
structure type to be specified. The parent type must already be a structure type
known to Typed Racket, either built-in or via
require/typed
. The
structure predicate has the appropriate Typed Racket filter type so that it may
be used as a predicate in
if
expressions in Typed Racket.
Examples:
module
UNTYPED
racket/base
define
100
struct
IntTree
elem
left
right
provide
struct-out
IntTree
module
TYPED
typed/racket
require/typed
UNTYPED
Natural
#:struct
IntTree
elem
Integer
left
IntTree
right
IntTree
The fourth case defines a new
opaque type
using the
function
pred
as a
predicate
(Module
must provide
pred
and
pred
must have type
Any
->
Boolean
.)
The type
is defined as precisely those values that
pred
returns
#t
for.
Opaque types must be required lexically before they are used.
Examples:
require/typed
racket/base
#:opaque
Evt
evt?
alarm-evt
Real
->
Evt
sync
Evt
->
Any
evt?
- : (-> Any Boolean : Evt)
#
sync
alarm-evt
100
current-inexact-milliseconds
- : Any
#
The
#:signature
keyword registers the required
signature in the signature environment. For more information on the use of
signatures in Typed Racket see the documentation for
typed/racket/unit
In all cases, the identifiers are protected with
contracts
which
enforce the specified types. If this contract fails, the module
is blamed.
Some types, notably the types of predicates such as
number?
cannot be converted to contracts and raise a static error when used in
require/typed
form. Here is an example of using
case->
in
require/typed
require/typed
racket/base
file-or-directory-modify-seconds
case->
String
->
Exact-Nonnegative-Integer
String
Option
Exact-Nonnegative-Integer
->
Exact-Nonnegative-Integer
Void
String
Option
Exact-Nonnegative-Integer
->
Any
->
Any
file-or-directory-modify-seconds
has some arguments which are optional,
so we need to use
case->
Changed in version 1.4 of package
typed-racket-lib
: Added the
#:type-name
option.
Changed in version 1.6: Added syntax for struct type variables, only works in unsafe requires.
Changed in version 1.12: Added default type
Any
for omitted
inst
args.
syntax
require/typed/provide
rt-clause
...
Similar to
require/typed
, but also provides the imported identifiers.
Uses outside of a module top-level raise an error.
Examples:
module
evts
typed/racket
require/typed/provide
racket/base
#:opaque
Evt
evt?
alarm-evt
Real
->
Evt
sync
Evt
->
Any
require
evts
sync
alarm-evt
100
current-inexact-milliseconds
- : Any
#
2.10
Other Forms
syntax
with-handlers
Identical to
with-handlers
from
racket/base
but provides additional annotations to assist the typechecker.
syntax
with-handlers*
Identical to
with-handlers*
from
racket/base
but provides additional annotations to assist the typechecker.
Added in version 1.12 of package
typed-racket-lib
procedure
default-continuation-prompt-tag
->
Prompt-Tagof
Any
Any
->
Any
Identical to
default-continuation-prompt-tag
, but additionally protects
the resulting prompt tag with a contract that wraps
higher-order values, such as functions, that are communicated with that
prompt tag. If the wrapped value is used in untyped code, a contract error
will be raised.
Examples:
module
typed
typed/racket
provide
do-abort
do-abort
->
Void
define
do-abort
abort-current-continuation
typed, and thus contracted, prompt tag
default-continuation-prompt-tag
λ:
Integer
module
untyped
racket
require
typed
call-with-continuation-prompt
do-abort
default-continuation-prompt-tag
the function cannot be passed an argument
require
untyped
default-continuation-prompt-tag: broke its own contract
Attempted to use a higher-order value passed as `Any` in
untyped code: #
in: the range of
(-> (prompt-tag/c Any #:call/cc Any))
contract from: untyped
blaming: untyped
(assuming the contract is correct)
syntax
#%module-begin
form
...
Legal only in a
module begin context
The
#%module-begin
form of
typed/racket
checks all the
forms in the module, using the Typed Racket type checking rules. All
provide
forms are rewritten to insert contracts where appropriate.
Otherwise, the
#%module-begin
form of
typed/racket
behaves like
#%module-begin
from
racket
syntax
#%top-interaction
form
Performs type checking of forms entered at the read-eval-print loop. The
#%top-interaction
form also prints the type of
form
after
type checking.
2.11
Special Structure Type Properties
value
prop:procedure
struct-type-property?
Unlike many other structure type properties,
prop:procedure
does not
have predefined types for its property values. When a structure is assocatied
with
prop:procedure
, its constructors’ return type is an intersection
type of the structure type and a function type specified by the property
value.
Examples:
struct
animal
Number
->
Number
Number
#:property
prop:procedure
struct-field-index
animal
add1
- : (∩ (-> Number Number) animal)
#
struct
plant
Number
#:property
prop:procedure
lambda
me
plant
a1
String
Number
plant-a
me
string-length
a1
plant
31
- : (∩ (-> String Number) plant)
#
In other words, a variable that refers to a
function is not allowed
Unlike in Racket, only one of the following types of expressions are allowed in
Typed Racket: a nonnegative literal,
struct-index-field
field-name
or a lambda expression. Note that in the last case, if the type annotation on
the codomain is not supplied, the type checker will use
Any
as the
return type.
Similar to other structure type properties, when a structure’s base structure
specifies a value for
prop:procedure
, the structure inherits that
value if it does not specify its own.
Examples:
struct
cat
animal
Number
cat
add1
42
- : (∩ (-> Number Number) cat)
#
struct
a-cat
cat
a-cat
add1
42
- : (∩ (-> Number Number) a-cat)
#
Function types for procedural structures do not enforce subtyping relations. A
substructure can specify a different field index or a procedure that has a
arity and/or types different from its base structures for
prop:procedure
Examples:
struct
b-cat
cat
->
Number
String
#:property
prop:procedure
struct-field-index
b-cat
add1
42
number->string
- : (∩ (-> Number String) b-cat)
#
top
contents
← prev
up
next →
US