[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: default-initargs question



In article <9504037995.AA799517170@NSDGATE3.NSD.FMC.COM>,
paul_hasse@NSDGATE3.nsd.fmc.com (paul hasse) wrote:

> I noticed that the following simple defclass does not cause an error:
> 
> (defclass person () () (:default-initargs :x 5 :x 6))
> 
> Of course, if you attempt to do a make-instance on person you get an error:
> 
>  (make-instance 'person)
> > Error: :X is an invalid initarg to INITIALIZE-INSTANCE for #<STANDARD-CLASS 
> PERSON>.
> >        Valid initargs: #().
> > While executing: CCL::CHECK-INITARGS
> > Type Command-. to abort.
> See the Restarts. menu item for further choices.
> 
> Isn't this bug in the class person detectable at the time of the defclass?

Nope. Default initargs don't always have to initialize a slot; they can
also be used to supply arguments to INITIALIZE-INSTANCE et al:

  (defclass employee (person) ((position)))

  (defmethod initialize-instance ((emp employee) &key x)
   ;; initialize position from :X initarg value
   (setf (slot-value emp 'position) (foobar x)))

The :DEFAULT-INITARGS class option provides a mechanism for the user to
give a default value form for an initialization argument without knowing
whether the initialization argument initializes a slot or is passed to a
method.

> Also, according to Steel, p. 825, I interpret that the defclass should have 
> caused an error because :x appears twice.  Is this a bug in MCL?

I'd say yes. ANSI spec says the same thing:

  "If an initialization argument name appears more than once
   in a :DEFAULT-INITARGS class option, an error is signaled."

pch