From e507d9a1a3ef1642a3fb74e30757d9f38d4ac6b2 Mon Sep 17 00:00:00 2001 From: Matthew Flatt Date: Thu, 21 Aug 2008 17:02:50 +0000 Subject: [PATCH] fix doc mistakes related to struct guards svn: r11368 --- .../scribblings/guide/define-struct.scrbl | 31 +++++++++++-------- collects/scribblings/reference/struct.scrbl | 4 +-- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/collects/scribblings/guide/define-struct.scrbl b/collects/scribblings/guide/define-struct.scrbl index e1fb06f771..53ca28594a 100644 --- a/collects/scribblings/guide/define-struct.scrbl +++ b/collects/scribblings/guide/define-struct.scrbl @@ -406,13 +406,16 @@ A @scheme[_struct-option] always starts with a keyword: @;-- FIXME: @;-- Explain when to use guards instead of contracts, and vice-versa - @specspecsubform[(code:line #:guard guard-expr)]{ - Specifies a @deftech{constructor guard} procedure to be called - whenever an instance of the structure type is created. The guard - takes as many arguments as non-automatic fields in the structure - type, and it should return the same number of values. The guard can - raise an exception if one of the given arguments is unacceptable, or - it can convert an argument. + @specspecsubform[(code:line #:guard guard-expr)]{ Specifies a + @deftech{constructor guard} procedure to be called whenever an + instance of the structure type is created. The guard takes as many + arguments as non-automatic fields in the structure type, plus one + more for the name of the instantiated type (in case a sub-type is + instantiated, in which case it's best to report an error using the + sub-type's name). The guard should return the same number of values + as given, minus the name argument. The guard can raise an exception + if one of the given arguments is unacceptable, or it can convert an + argument. @defexamples[ #:eval posn-eval @@ -421,12 +424,14 @@ A @scheme[_struct-option] always starts with a keyword: #:guard (lambda (name type-name) (cond [(string? name) name] - [(number? name) - (number->string name)] - [else (error "bad name" name)]))) + [(symbol? name) (symbol->string name)] + [else (error type-name + "bad name: ~e" + name)]))) (make-thing "apple") + (make-thing 'apple) (make-thing 1/2) - (make-thing #f)] + ] The guard is called even when subtype instances are created. In that case, only the fields accepted by the constructor are provided to @@ -439,11 +444,11 @@ A @scheme[_struct-option] always starts with a keyword: #:transparent #:guard (lambda (name age type-name) (if (negative? age) - (error "bad age" age) + (error type-name "bad age: ~e" age) (values name age)))) (make-person "John" 10) (make-person "Mary" -1) - (make-person #f 10)]} + (make-person 10 10)]} @specspecsubform[(code:line #:property prop-expr val-expr)]{ Associates a @deftech{property} and value with the structure type. diff --git a/collects/scribblings/reference/struct.scrbl b/collects/scribblings/reference/struct.scrbl index 7e3638c78f..275e5cd80a 100644 --- a/collects/scribblings/reference/struct.scrbl +++ b/collects/scribblings/reference/struct.scrbl @@ -105,7 +105,7 @@ structures depends on the current inspector.) Creates a new structure type, unless @scheme[inspector] is @scheme['prefab], in which case @scheme[make-struct-type] accesses a -@techlink{prefab} structre type. The @scheme[name] argument is used +@techlink{prefab} structure type. The @scheme[name] argument is used as the type name. If @scheme[super-type] is not @scheme[#f], the resulting type is a subtype of the corresponding structure type. @@ -147,7 +147,7 @@ positions. Each element in the list must be unique, otherwise @scheme[0] (inclusive) to @scheme[init-field-cnt] (exclusive), otherwise @exnraise[exn:fail:contract]. -The @scheme[guard] argument is either a procedure of @math{n} +The @scheme[guard] argument is either a procedure of @math{n+1} arguments or @scheme[#f], where @math{n} is the number of arguments for the new structure type's constructor (i.e., @scheme[init-field-cnt] plus constructor arguments implied by