Add some examples for cast.

This commit is contained in:
Sam Tobin-Hochstadt 2012-08-21 10:48:44 -04:00
parent 1248b32e73
commit 71e81f9fff
2 changed files with 14 additions and 4 deletions

View File

@ -279,8 +279,10 @@ cycles among them are prohibited.
@section{Generating Predicates Automatically} @section{Generating Predicates Automatically}
@defform[(make-predicate t)]{ @defform[(make-predicate t)]{
Evaluates to a predicate for the type @racket[t], with the type Evaluates to a predicate for the type @racket[t], with the type
@racket[(Any -> Boolean : t)]. @racket[t] may not contain function types.} @racket[(Any -> Boolean : t)]. @racket[t] may not contain function types, or
types that may refer to mutable data such as @racket[(Vectorof Integer)].}
@defform[(define-predicate name t)]{ @defform[(define-predicate name t)]{
Equivalent to @racket[(define name (make-predicate t))]. Equivalent to @racket[(define name (make-predicate t))].
@ -308,15 +310,21 @@ This is legal only in expression contexts.}
@defform[(cast e t)]{The entire expression has the type @racket[t], while @defform[(cast e t)]{The entire expression has the type @racket[t], while
@racket[e] may have any type. The value of the entire expression is the value @racket[e] may have any type. The value of the entire expression is the value
returned by @racket[e], protected by a contract ensuring that it has type returned by @racket[e], protected by a contract ensuring that it has type
@racket[t]. This is legal only in expression contexts.} @racket[t]. This is legal only in expression contexts.
@ex[(cast 3 Integer)
(cast 3 String)
(cast (lambda: ([x : Any]) x) (String -> String))
]
}
@defform[(inst e t ...)]{Instantiate the type of @racket[e] with types @defform[(inst e t ...)]{Instantiate the type of @racket[e] with types
@racket[t ...]. @racket[e] must have a polymorphic type with the @racket[t ...]. @racket[e] must have a polymorphic type with the
appropriate number of type variables. This is legal only in expression appropriate number of type variables. This is legal only in expression
contexts. contexts.
@ex[(foldl (inst cons Integer Integer) null (list 1 2 3 4))] @ex[(foldl (inst cons Integer Integer) null (list 1 2 3 4))
@ex[(: fold-list : (All (A) (Listof A) -> (Listof A))) (: fold-list : (All (A) (Listof A) -> (Listof A)))
(define (fold-list lst) (define (fold-list lst)
(foldl (inst cons A A) null lst)) (foldl (inst cons A A) null lst))

View File

@ -18,6 +18,8 @@ Typed Racket provides some additional utility functions to facilitate typed prog
Verifies that the argument satisfies the constraint. If no predicate Verifies that the argument satisfies the constraint. If no predicate
is provided, simply checks that the value is not is provided, simply checks that the value is not
@racket[#f]. @racket[#f].
See also the @racket[cast] form.
} }
@examples[#:eval the-top-eval @examples[#:eval the-top-eval