From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: ludovic.courtes@laas.fr (Ludovic =?iso-8859-1?Q?Court=E8s?=) Newsgroups: gmane.lisp.guile.devel Subject: [PATCH] Document pitfalls with `define-class' and `#:init-value' Date: Wed, 01 Mar 2006 17:20:12 +0100 Organization: LAAS-CNRS Message-ID: <87oe0qt9vn.fsf@laas.fr> NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: sea.gmane.org 1141501560 3125 80.91.229.2 (4 Mar 2006 19:46:00 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Sat, 4 Mar 2006 19:46:00 +0000 (UTC) Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Sat Mar 04 20:45:57 2006 Return-path: Envelope-to: guile-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by ciao.gmane.org with esmtp (Exim 4.43) id 1FFchN-0000iq-0z for guile-devel@m.gmane.org; Sat, 04 Mar 2006 20:45:53 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1FFchR-0006W2-4k for guile-devel@m.gmane.org; Sat, 04 Mar 2006 14:45:57 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1FFaOh-000349-HQ for guile-devel@gnu.org; Sat, 04 Mar 2006 12:18:28 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1FFaOc-0002yy-NM for guile-devel@gnu.org; Sat, 04 Mar 2006 12:18:25 -0500 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1FEi8h-0004pp-HX for guile-devel@gnu.org; Thu, 02 Mar 2006 02:22:19 -0500 Original-Received: from [140.93.0.15] (helo=laas.laas.fr) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA:32) (Exim 4.52) id 1FEU5N-0003F2-NZ for guile-devel@gnu.org; Wed, 01 Mar 2006 11:21:58 -0500 Original-Received: by laas.laas.fr (8.13.1/8.13.4) with SMTP id k21GKYSM022119; Wed, 1 Mar 2006 17:20:36 +0100 (CET) Original-To: Guile-Devel X-URL: http://www.laas.fr/~lcourtes/ X-Revolutionary-Date: 11 =?iso-8859-1?Q?Vent=F4se?= an 214 de la =?iso-8859-1?Q?R=E9volution?= X-PGP-Key-ID: 0xEB1F5364 X-PGP-Key: http://www.laas.fr/~lcourtes/ludovic.asc X-PGP-Fingerprint: 821D 815D 902A 7EAB 5CEE D120 7FBA 3D4F EB1F 5364 X-OS: powerpc-unknown-linux-gnu Mail-Followup-To: Guile-Devel User-Agent: Gnus/5.110004 (No Gnus v0.4) Emacs/21.4 (gnu/linux) X-Spam-Score: 0 () X-Scanned-By: MIMEDefang at CNRS-LAAS X-MIME-Autoconverted: from 8bit to quoted-printable by laas.laas.fr id k21GKYSM022119 X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.devel:5754 Archived-At: Hi, The patch below documents the following "issue": ;; What you have in mind is that the `blurps' slot will always refer ;; to a new list. guile> (define-class () (blurps :init-value (list 0))) guile> (define c (make )) guile> (set-car! (slot-ref c 'blurps) 1) guile> (slot-ref c 'blurps) (1) ;; Now, see what happens when a new instance is created... guile> (define c2 (make )) guile> (set-car! (slot-ref c2 'blurps) 7) guile> (slot-ref c 'blurps) (7) Conclusion: the `init-value' is shared across instance of the class. I believe this is intentional given that we have `#:init-thunk' to work around this problem. The patch below is based around this assumption. Thanks, Ludovic. 2006-03-01 Ludovic Court=E8s * goops.texi (Slot Options): Explain the single-instance pitfall with `#:init-value' and how to work around it. --- orig/doc/goops/goops.texi +++ mod/doc/goops/goops.texi @@ -833,12 +833,40 @@ @deffnx {slot option} #:init-keyword init-keyword These options provide various ways to specify how to initialize the slot's value at instance creation time. @var{init-value} is a fixed -value. @var{init-thunk} is a procedure of no arguments that is called -when a new instance is created and should return the desired initial -slot value. @var{init-form} is an unevaluated expression that gets +value, @emph{shared across all new instances of the class}. +@var{init-thunk} is a procedure of no arguments that is called 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 the @code{init-value} is shared across new instances +of a class, you may only use it when the initial value is an immutable +value, like a constant. If you want to initialize a slot with a +fresh, mutable value, you should use @code{init-thunk} instead to make +sure that each new instance's slot is initialized with a new object. +Consider the following example: + +@example +(define-class () + (hashtab #:init-value (make-hash-table))) +@end example + +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{} initialized with a new hash +table, you have to proceed as follow: + +@example +(define-class () + (hashtab #:init-thunk make-hash-table)) +@end example + +Here, @code{make-hash-table} will be called each time a new instance +of @code{} is created, thus initializing each @code{hashtab} +slot with a new hash table. =20 If more than one of these options is specified for the same slot, the order of precedence, highest first is _______________________________________________ Guile-devel mailing list Guile-devel@gnu.org http://lists.gnu.org/mailman/listinfo/guile-devel