🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.clos
Article #290

Re: clos initforms'

#290
From: Barry Margolin
Date: Tue, 10 Nov 1992 17:54
56 lines
2396 bytes
In article <BxHA1I.G6D@world.std.com> jbk@world.std.com (Jeffrey B Kane) writes:
>(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 you got that error is because you mistyped the name of the
variable, leaving out the hyphen.  However, had you spelled it correctly
you would probably have gotten something like:

>>Error: The value of S, 4, should be a SYMBOL

BOUNDP:
   Required arg 0 (S): 4

The problem here is that you didn't quote the symbol argument to BOUNDP.

Fixing that, however, wouldn't solve your problem either.  BOUNDP can only
be used to test dynamic variables, not lexical variables; the latter are
always bound.  Also, the default value for unsupplied keyword arguments is
NIL, not unbound.

>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?

Use a supplied-p parameter (called "svar" on p.76-80 of CLtL2):

(defun make-test-class (&key (slot-1 3) (slot-2 nil slot-2-supplied-p))
  (unless slot-2-supplied-p
    (setf slot-2 slot-1))
  (make-instance 'test-class :slot-1 slot-1 :slot-2 slot-2))

Also, the initform for optional and keyword parameters is evaluated in an
environment that includes the bindings of all the arguments preceding it in
the lambda list, so for this particular case you could write:

(defun make-test-class (&key (slot-1 3) (slot-2 slot-1))
  (make-instance 'test-class :slot-1 slot-1 :slot-2 slot-2))
-- 
Barry Margolin
System Manager, Thinking Machines Corp.

barmar@think.com          {uunet,harvard}!think!barmar
Technical Details
Message-ID: <1dot03INN5uk@early-bird.think.com>
Path: arc1-ca.pugleaf.net!sparky!uunet!caen!sdd.hp.com!think.com!barmar
References: <BxHA1I.G6D@world.std.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!caen!sdd.hp.com!think.com!barmar
From: barmar@think.com (Barry Margolin)
Newsgroups: comp.lang.clos
Subject: Re: clos initforms'
Date: 10 Nov 1992 17:54:11 GMT
Organization: Thinking Machines Corporation, Cambridge MA, USA
Lines: 56
Message-ID: <1dot03INN5uk@early-bird.think.com>
References: <BxHA1I.G6D@world.std.com>
NNTP-Posting-Host: telecaster.think.com