From 71e81f9fffa682f93b82dc76c51b92606884d24a Mon Sep 17 00:00:00 2001 From: Sam Tobin-Hochstadt Date: Tue, 21 Aug 2012 10:48:44 -0400 Subject: [PATCH] Add some examples for `cast`. --- .../scribblings/reference/special-forms.scrbl | 16 ++++++++++++---- .../scribblings/reference/utilities.scrbl | 2 ++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/collects/typed-racket/scribblings/reference/special-forms.scrbl b/collects/typed-racket/scribblings/reference/special-forms.scrbl index d3f6244c17..1c0e3fc733 100644 --- a/collects/typed-racket/scribblings/reference/special-forms.scrbl +++ b/collects/typed-racket/scribblings/reference/special-forms.scrbl @@ -279,8 +279,10 @@ cycles among them are prohibited. @section{Generating Predicates Automatically} @defform[(make-predicate t)]{ + 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)]{ 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 @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 -@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 @racket[t ...]. @racket[e] must have a polymorphic type with the appropriate number of type variables. This is legal only in expression 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) (foldl (inst cons A A) null lst)) diff --git a/collects/typed-racket/scribblings/reference/utilities.scrbl b/collects/typed-racket/scribblings/reference/utilities.scrbl index fbb0be984c..43f9ff66b9 100644 --- a/collects/typed-racket/scribblings/reference/utilities.scrbl +++ b/collects/typed-racket/scribblings/reference/utilities.scrbl @@ -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 is provided, simply checks that the value is not @racket[#f]. + +See also the @racket[cast] form. } @examples[#:eval the-top-eval