From 5ce6ee75df2e05aadaf29f8a2c1bfabd7f123cbd Mon Sep 17 00:00:00 2001 From: Neil Jerram Date: Thu, 28 Sep 2006 07:55:05 +0000 Subject: [PATCH] =?UTF-8?q?(Slot=20Options):=20Added=20example=20from=20Lu?= =?UTF-8?q?dovic=20Court=C3=A8s=20about=20difference=20between=20init-valu?= =?UTF-8?q?e,=20-form=20and=20-thunk.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/goops/ChangeLog | 5 +++++ doc/goops/goops.texi | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/doc/goops/ChangeLog b/doc/goops/ChangeLog index f9c43e60b..b0e742b71 100644 --- a/doc/goops/ChangeLog +++ b/doc/goops/ChangeLog @@ -1,3 +1,8 @@ +2006-09-28 Neil Jerram + + * goops.texi (Slot Options): Added example from Ludovic Courtès + about difference between init-value, -form and -thunk. + 2006-04-21 Kevin Ryde * hierarchy.pdf: New file, converted from hierarchy.eps using diff --git a/doc/goops/goops.texi b/doc/goops/goops.texi index 171ac2bfa..d6d8e595d 100644 --- a/doc/goops/goops.texi +++ b/doc/goops/goops.texi @@ -838,8 +838,40 @@ value (shared across all new instances of the class). when a new instance is created and should return the desired initial slot value. @var{init-form} is an unevaluated expression that gets evaluated when a new instance is created and should return the desired -initial slot value. @var{init-keyword} is a keyword that can be used to -pass an initial slot value to @code{make} when creating a new instance. +initial slot value. @var{init-keyword} is a keyword that can be used +to pass an initial slot value to @code{make} when creating a new +instance. + +Note that, since an @code{init-value} value is shared across all +instances of a class, you should only use it when the initial value is +an immutable value, like a constant. If you want to initialize a slot +with a fresh, independently mutable value, you should use +@code{init-thunk} or @code{init-form} instead. Consider the following +example. + +@example +(define-class () + (hashtab #:init-value (make-hash-table))) +@end example + +@noindent +Here only one hash table is created and all instances of +@code{} have their @code{hashtab} slot refer to it. In order +to have each instance of @code{} refer to a new hash table, you +should instead write: + +@example +(define-class () + (hashtab #:init-thunk make-hash-table)) +@end example + +@noindent +or: + +@example +(define-class () + (hashtab #:init-form (make-hash-table))) +@end example If more than one of these options is specified for the same slot, the order of precedence, highest first is