Thread View: comp.lang.clos
1 messages
1 total messages
Started by john
Tue, 10 Nov 1992 14:53
Re: clos initforms
Author: 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
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads