Lisp HUG Maillist Archive

default-initargs and implicit instantiation of superclasses

Unable to parse email body. Email id is 4104

Re: default-initargs and implicit instantiation of superclasses

The abstract superclass is not instantiated, it's just that your  
initialize-instance method is called because of method inheritance.

You probably want a method on make-instance:

(defmethod make-instance :before ((class (eql 'abstract)) &key)
   (error "abstract class instantiation"))

This would ensure that subclasses can be instantiated. However, ANSI  
CL says that it's unspecified to specialize generic functions on  
existing classes. 'abstract is a symbol, so this case is  
unfortunately covered.

A conformant way out is to define a metaclass abstract-class, so that  
you can say:

(defclass foo ()
   ()
   (:metaclass abstract-class))

This would do the job provided you define this:

(defmethod make-instance :before ((class abstract-class))
   (error "abstract class instantiation"))


I hope this helps.


Pascal


On 12 Jul 2005, at 12:05, Nick Levine wrote:

> Is it correct that the presence of default-initargs may cause
> superclasses to be instantiated?
>
> - nick
>
>
>   CL-USER 48 > (defclass abstract ()
>          ((flip :initarg :flip)))
>   #<STANDARD-CLASS ABSTRACT 2A700F04>
>
>   CL-USER 49 > (defmethod initialize-instance :after ((self  
> abstract) &key)
>          (error "Please don't instantiate abstract classes."))
>   #<STANDARD-METHOD INITIALIZE-INSTANCE (:AFTER) (ABSTRACT) 28FC1DAC>
>
>   CL-USER 50 > (defclass concrete (abstract)
>          ()
>          (:default-initargs :flip :flop))
>   #<STANDARD-CLASS CONCRETE 2A701364>
>
>   CL-USER 51 > (make-instance 'concrete)
>
>   Error: Please don't instantiate abstract classes.
>     1 (abort) Return to level 0.
>     2 Return to top loop level 0.
>
>

--
2nd European Lisp and Scheme Workshop
July 26 - Glasgow, Scotland - co-located with ECOOP 2005
http://lisp-ecoop05.bknr.net/



Re: default-initargs and implicit instantiation of superclasses

Unable to parse email body. Email id is 4106

Re: default-initargs and implicit instantiation of superclasses

Hi Nick,

> Is it correct that the presence of default-initargs may cause
> superclasses to be instantiated?

The default-initargs class option doesn't make any difference here:


CL-USER 1 > (defclass abstract ()
              ((flip :initarg :flip)))
#<STANDARD-CLASS ABSTRACT 20692644>

CL-USER 2 > (defmethod initialize-instance :after ((self abstract) &key)
              (error "Please don't instantiate abstract classes."))
#<STANDARD-METHOD INITIALIZE-INSTANCE (:AFTER) (ABSTRACT) 20691894>

CL-USER 3 > (defclass concrete (abstract)
              ())
#<STANDARD-CLASS CONCRETE 20681D94>

CL-USER 4 > (make-instance 'concrete)

Error: Please don't instantiate abstract classes.
  1 (abort) Return to level 0.
  2 Return to top loop level 0.


The INITIALIZE-INSTANCE after method gets called either way (which
is the way it should be, I think).  You may want to rethink your
strategy.


Arthur


Re: default-initargs and implicit instantiation of superclasses

Unable to parse email body. Email id is 4108

Re: default-initargs and implicit instantiation of superclasses

Unable to parse email body. Email id is 4110

Updated at: 2020-12-10 08:51 UTC