Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Common Lisp

Name: Anonymous 2011-05-26 16:37

has no structure-p or structure-slots

Name: Anonymous 2011-05-27 7:04

>>1
Of course it does, but it's not perfect(won't work if you changed the type to list or vector):
(typep your-structure 'structure-object)
And you can use slot-value and to find the slots, you can just use the MOP:

CL-USER> (defstruct lol a b c)
LOL
CL-USER> (mop:class-slots (class-of (make-lol)))
(#<SB-PCL::STRUCTURE-EFFECTIVE-SLOT-DEFINITION A>
 #<SB-PCL::STRUCTURE-EFFECTIVE-SLOT-DEFINITION B>
 #<SB-PCL::STRUCTURE-EFFECTIVE-SLOT-DEFINITION C>)
CL-USER> (mapcar #'mop:slot-definition-name *)
(A B C)
CL-USER> (slot-value (make-lol :c 3) 'c)
3


Do you know why CL doesn't have a structure-p or structure-slots?
Because structures can be implemented without CLOS in some implementations and because you can specify their type to be a list of a vector. Structures are the precursor of objects. If it's a list or a vector, you won't be able to tell them apart.
However, structures do let you define a struct-name-p function for each structure you define, which would work regardless of the type you use (which checks the car or first item in the vector, if the type is that). However, since MOP isn't in all the CL implementations, you should just use CLOS instead of structures if you are not satisfied with the struct-name-p defined by defstruct.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List