Article View: comp.lang.clos
Article #289Re: clos initforms'
From: kanderso
Date: Tue, 10 Nov 1992 04:32
Date: Tue, 10 Nov 1992 04:32
72 lines
3350 bytes
3350 bytes
To: comp.lang.clos@cis.ohio-state.edu
Date: Tue, 10 Nov 1992 02:10:29 GMT
Organization: The World Public Access UNIX, Brookline, MA
From: Jeffrey B Kane <jbk@world.std.com>
Sender: commonloops-request@cis.ohio-state.edu
Subject: Re: clos initforms'
>Try using an :after method for initialize-instance on this class.
>E.g., (not tested),
>(defmethod initialize-instance :after ((new test-class) &key)
> (unless (slot-boundp new 'slot-2)
> (setf (slot-2 new) (slot-1 new))))
While, i think the above is the right approach, for completeness, one could
also delay initializing slot-2 until it was accessed:
(defmethod slot-2 ((tc test-class))
(if (slot-boundp tc 'slot-2) (slot-value tc 'slot-2)
(if (slot-boundp tc 'slot-1) (setf (slot-value tc 'slot-2)
(slot-value tc 'slot-1))
(slot-value tc 'slot-2)))) ; Lose Lose.
Dan,
This would work, however after looking at the various ways that I
should handle this, it looks like I should define a constructor
function, then check if various key words have been defined, i.e.
(defclass test-class ()
((slot-1 :initarg :slot-1
:accessor slot-1)
(slot-2 :initarg :slot-2
:accessor slot-2)))
(defun make-test-class (&key (slot-1 3) slot-2)
(unless (boundp slot2)
(setf slot-2 slot-1))
(make-instance 'test-class :slot-1 slot-1 :slot-2 slot-2))
The problem with this approach is that the function "boundp"
does not test if the user typed in a particular key word.
Calling:
(make-test-class :slot-2 4)
results in the error:
> Error: Unbound variable: SLOT2
> While executing: SYMBOL-VALUE
The reason I think that the constructor approach is bettter is than
using initialize-instance :after is that it allows other key word and
a higher level of abstraction. The the bottom line question is:
I really think using a constructor and an initialize-instance :after are
separate issues. For example, your make-test-class constructor contains
knowledge about the class test-class. If you now defined a class
test-class-2 as a subclass of test-class, and built the corresponding
constructor make-test-class-2, you would have to duplicate that knowledge.
Using initialize-instance :after, lets you specify that knowledge in one place.
So, i'd recommend using the initialize-instance :after approach to concentrate
knowledge about your classes in one place, and also the constructor
approach if you wanted to hide everything from your users.
Other than defining a keyword with some kind of a value to flag it
hasn't been used, how can you test if it was used similar to slot-boundp
for slots?
(defun make-test-class (&key (slot-1 3) (slot-2 nil slot-2-bound-p)) ...
Technical Details
Message-ID:
<9211101435.AA03435@news.cis.ohio-state.edu>Path:
arc1-ca.pugleaf.net!sparky!uunet!zaphod.mps.ohio-state.edu!magnus.acs.ohio-state.edu!cis.ohio-state.edu!bbn.com!kandersoReferences:
<BxHA1I.G6D@world.std.com>Show Full Headers
Complete Article Headers
Newsgroups: comp.lang.clos Path: arc1-ca.pugleaf.net!sparky!uunet!zaphod.mps.ohio-state.edu!magnus.acs.ohio-state.edu!cis.ohio-state.edu!bbn.com!kanderso From: kanderso@bbn.com Subject: Re: clos initforms' Message-ID: <9211101435.AA03435@news.cis.ohio-state.edu> Sender: deanna@cis.ohio-state.edu (Deanna Lynn Tavenner) Organization: The Ohio State University Department of Computer and Information Science References: <BxHA1I.G6D@world.std.com> Date: Tue, 10 Nov 1992 04:32:29 GMT Lines: 72