Article View: comp.lang.clos
Article #288Re: clos initforms
From: john
Date: Tue, 10 Nov 1992 14:53
Date: Tue, 10 Nov 1992 14:53
56 lines
2254 bytes
2254 bytes
jbk@world.std.com (Jeffrey B Kane) writes:
(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
Uh, that's 'cause you mistyped SLOT-2 as SLOT2. But the variable SLOT-2 would
be bound in any case. You can't use BOUNDP to tell if a keyword or optional
parameter was supplied, because they have default values.
That aside:
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.
But it doesn't allow you to specialize the class. And it assumes that
SLOT-1 is only initialized in one way, through the initarg :SLOT-1.
Hardly more abstract.
The the bottom line question is:
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?
I'm not sure what this means, but are you asking how to tell if a
keyword was used? Use what CLtL calls "svars" (page 76, CLtL2),
presumably "s" for "supplied". Minimally modifying your constructor:
(defun make-test-class (&key (slot-1 3) (slot-2 NIL SLOT-2-SUPPLIED?)
(unless SLOT-2-SUPPLIED?
(setf slot-2 slot-1))
(make-instance 'test-class :slot-1 slot-1 :slot-2 slot-2))
John Burger
john@mitre.org
Technical Details
Message-ID:
<9211101453.AA06154@thelonius.mitre.org>Path:
arc1-ca.pugleaf.net!sparky!uunet!dtix!darwin.sura.net!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!linus.mitre.org!johnShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!dtix!darwin.sura.net!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!linus.mitre.org!john From: john@linus.mitre.org Newsgroups: comp.lang.clos Subject: Re: clos initforms Message-ID: <9211101453.AA06154@thelonius.mitre.org> Date: 10 Nov 92 14:53:22 GMT Sender: daemon@cis.ohio-state.edu Organization: The Ohio State University Department of Computer and Information Science Lines: 56