Re: make-instance without initialize-instance



TKP> I think that I have to call allocate-instance, but when I try

TKP> (allocate-instance (find-class 'my-class))

TKP> I get an error condition (which SBCL is "unable to display", so I must
TKP> have screwed up heavily). What's the proper way to do what I want?

Well, allocate-instance itself works fine:

CL-USER> (defclass test1 () (a))
#<STANDARD-CLASS TEST1>
CL-USER> (allocate-instance (find-class 'test1))
#<TEST1 {C5C9AA1}>


If it doesn't work with your class, it is some problem you have in your code.

E.g. one way to implement this error:

CL-USER> (defmethod print-object ((o test1) stream)
(format stream "#<TEST1 a=~a>" (slot-value o 'a)))

CL-USER> (allocate-instance (find-class 'test1))

Unable to display error condition
[Condition of type UNBOUND-SLOT]
0: ((SB-PCL::FAST-METHOD SLOT-UNBOUND (T T T)) #<error printing object>)
1: (SB-PCL::SLOT-UNBOUND-INTERNAL #<error printing object>)
2: ((SB-PCL::FAST-METHOD PRINT-OBJECT
(TEST1 T)) #<error printing object>)
3: (SB-PRETTY:OUTPUT-PRETTY-OBJECT #<error printing object>)
4: (SB-IMPL::STRINGIFY-OBJECT #<error printing object>)
5: (PRIN1-TO-STRING #<error printing object>)


That is, forget to initialize a slot and write a print-object which uses that slot, and you'll get "Unable to display" thing.

By the way, you might want to use shared-initialize (it is more flexible than initialize-instance) after allocate-instance, but it is not strictly necessary, depends on your goal.

.



Relevant Pages