Thread View: comp.lang.clos
4 messages
4 total messages
Started by Jeffrey B Kane
Mon, 09 Nov 1992 05:35
clos initforms
Author: Jeffrey B Kane
Date: Mon, 09 Nov 1992 05:35
Date: Mon, 09 Nov 1992 05:35
48 lines
1907 bytes
1907 bytes
I was wondering if someone could suggest the best solution
for the following problem:
I have a class with several slots As an example:
(defclass test-class ()
((slot-1 :initarg :slot-1
:initform 1
:accessor slot-1)
(slot-2 :initarg :slot-2
:initform ????? ; <- The question
:accessor slot-2)))
The problem is that I need for slot-2 to default to the same value as
slot-1 if the user does not specify it in the constructor.
(setf my-class-1 (make-instance 'test-class :slot-1 12))
(setf my-class-2 (make-instance 'test-class :slot-1 12 :slot-2 18))
what I want is a generic way to have slot-2 = slot-1 in the first case and
slot-2 = 18 (the supplied value) in the second. I'm looking for a general
enough solution so I don't have to do something like:
(defclass test-class ()
((slot-1 :initarg :slot-1
:accessor slot-1)
(slot-2 :initarg :slot-2
:accessor slot-2)))
(defun make-test-class ((me test-class) &key (:slot-1 1) (:slot-2 nil))
(unless slot-2
(setf slot-2 slot1)) ; if no default value for slot-2 use
slot-1's value
(make-instance 'test-class :slot-1 slot-1 :slot-2 slot2))
The problem I see with this approach is that it does not let you specify a nil
value for
slot-2. What I'm looking for is a general solution that doesn't lock out some
previously
defined "bad" value (nil in the above example).
Jeffrey
===============
Jeffrey Kane, MD
Kane Biomedical Systems
Boston MA
clos initforms
Author: Eric Peterson x7644
Date: Mon, 09 Nov 1992 15:49
Date: Mon, 09 Nov 1992 15:49
39 lines
1855 bytes
1855 bytes
Jeffrey> (defclass test-class ()
Jeffrey> ((slot-1 :initarg :slot-1
Jeffrey> :initform 1
Jeffrey> :accessor slot-1)
Jeffrey> (slot-2 :initarg :slot-2
Jeffrey> :initform ????? ; <- The question
Jeffrey> :accessor slot-2)))
Jeffrey> The problem is that I need for slot-2 to default to the same value as
Jeffrey> slot-1 if the user does not specify it in the constructor.
I believe the following will do what you want provided that you are
willing to call with :slot-2 first when both initargs are used (see
CLtL2 28.1.9.4. first paragraph after the last bullet):
(defclass test-class ()
((slot-1 :initarg :slot-1
:initform 1
:accessor slot-1)
(slot-2 :initarg :slot-1 :initarg :slot-2
:accessor slot-2)))
Required call order for non-defaulting case:
(make-instance 'test-clas :slot-2 5 :slot-1 3 )
I hope this helps.
Eric Peterson (speaking for myself)
Systems Research and Applications
2000 15th Street North
Arlington, VA 22201
USA
peterson@sra.com
(703) 558-7644
Fax: (703) 558-4723
Re: clos initforms
Author: Jose E. Hernandez
Date: Mon, 09 Nov 1992 16:43
Date: Mon, 09 Nov 1992 16:43
39 lines
1650 bytes
1650 bytes
|> I have a class with several slots As an example:
|>
|> (defclass test-class ()
|> ((slot-1 :initarg :slot-1
|> :initform 1
|> :accessor slot-1)
|> (slot-2 :initarg :slot-2
|> :initform ????? ; <- The question
|> :accessor slot-2)))
|>
|> The problem is that I need for slot-2 to default to the same value as
|> slot-1 if the user does not specify it in the constructor.
|>
|> (setf my-class-1 (make-instance 'test-class :slot-1 12))
|>
|> (setf my-class-2 (make-instance 'test-class :slot-1 12 :slot-2 18))
|>
|> Jeffrey Kane, MD
|> Kane Biomedical Systems
|> Boston MA
(defparameter *default-test-class-slot-value* 1)
(defclass test-class ()
((slot-1 :initarg :slot-1
:initform *default-test-class-slot-value*
:accessor slot-1)
(slot-2 :initarg :slot-2
:initform *default-test-class-slot-value*
:accessor slot-2)))
(defmethod shared-initialize ((inst test-class) slots &rest initargs
&key slot-1 slot-2)
(if (and slot-1 (not slot-2))
(apply #'call-next-method inst slots :slot-2 slot-1 initargs)
(call-next-method)))
jeh
Re: clos initforms
Author: Russell G. Almond
Date: Tue, 10 Nov 1992 20:15
Date: Tue, 10 Nov 1992 20:15
39 lines
1738 bytes
1738 bytes
Jeffery Asks:
> (defun make-test-class ((me test-class) &key (:slot-1 1) (:slot-2 nil))
> (unless slot-2
> (setf slot-2 slot1)) ; if no default value for slot-2 use
> ; slot-1's value
> (make-instance 'test-class :slot-1 slot-1 :slot-2 slot2))
> The problem I see with this approach is that it does not let you specify a nil
> value for
> slot-2. What I'm looking for is a general solution that doesn't lock out some
> previously
> defined "bad" value (nil in the above example).
> Jeffrey
I think you want the third (suppliedp) argument for &key (and &optional)
parameters in a lambda list. Thus:
(defun make-test-class ((me test-class) &key (:slot-1 1)
(:slot-2 nil slot-2?))
(unless slot-2?
(setf slot-2 slot1)) ; if no default value for slot-2 use
; slot-1's value
(make-instance 'test-class :slot-1 slot-1 :slot-2 slot2))
This should do what you want. (There should be an equivalent way to to
it with a shared-initalize method, I'm not as familiar with that
approach.)
Russell Almond
Statistical Sciences, Inc.
1700 Westlake Ave., N Suite 500
Seattle, WA 98109
(206) 283-8802
almond@statsci.com
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