🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.clos
Article #280

clos initforms

#280
From: Jeffrey B Kane
Date: Mon, 09 Nov 1992 05:35
48 lines
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
Technical Details
Message-ID: <BxFov6.8J5@world.std.com>
Path: arc1-ca.pugleaf.net!sparky!uunet!think.com!spool.mu.edu!sol.ctr.columbia.edu!eff!world!jbk

Show Full Headers
Complete Article Headers
Newsgroups: comp.lang.clos
Path: arc1-ca.pugleaf.net!sparky!uunet!think.com!spool.mu.edu!sol.ctr.columbia.edu!eff!world!jbk
From: jbk@world.std.com (Jeffrey B Kane)
Subject: clos initforms
Message-ID: <BxFov6.8J5@world.std.com>
Summary: defining defaults in terms of one another
Keywords: INITFORM DEFAULT-INITARG INITIALIZE-INSTANCE
Organization: The World Public Access UNIX, Brookline, MA
Date: Mon, 9 Nov 1992 05:35:30 GMT
Lines: 50