added examples to struct-copy docs

This commit is contained in:
Robby Findler 2011-03-25 11:02:22 -05:00
parent 614ec41ab7
commit 4e81299e98

View File

@ -3,6 +3,7 @@
(for-label racket/struct-info)) (for-label racket/struct-info))
@(define struct-eval (make-base-eval)) @(define struct-eval (make-base-eval))
@(define struct-copy-eval (make-base-eval))
@title[#:tag "structures" #:style 'toc]{Structures} @title[#:tag "structures" #:style 'toc]{Structures}
@ -422,7 +423,32 @@ structure instance is created.
The result of @racket[struct-expr] can be an instance of a sub-type of The result of @racket[struct-expr] can be an instance of a sub-type of
@racket[id], but the resulting copy is an immediate instance of @racket[id], but the resulting copy is an immediate instance of
@racket[id] (not the sub-type).} @racket[id] (not the sub-type).
@examples[
#:eval struct-copy-eval
(struct fish (color weight) #:transparent)
(define marlin (fish 'orange-and-white 11))
(define dory (struct-copy fish marlin
[color 'blue]))
dory
(struct shark fish (weeks-since-eating-fish) #:transparent)
(define bruce (shark 'grey 110 3))
(define chum (struct-copy shark bruce
[weight #:parent fish 90]
[weeks-since-eating-fish 0]))
chum
(code:comment "subtypes can be copied as if they were supertypes,")
(code:comment "but the result is an instance of the supertype")
(define not-really-chum
(struct-copy fish bruce
[weight 90]))
not-really-chum
]
}
@;------------------------------------------------------------------------ @;------------------------------------------------------------------------
@section[#:tag "structutils"]{Structure Utilities} @section[#:tag "structutils"]{Structure Utilities}
@ -716,3 +742,4 @@ identifiers. The two subsets correspond to @racket[#:auto] fields.}
@; ---------------------------------------------------------------------- @; ----------------------------------------------------------------------
@close-eval[struct-eval] @close-eval[struct-eval]
@close-eval[struct-copy-eval]