Common Lisp: How to check if a slot is bound? (CLOS)
Asked Answered
P

1

6

Say we have a slot without :initform

(defclass foo ()
  ((x :reader x :initarg x)))

How can I check if slot x of an instance of foo is bound?

There is a way to do this with MOP, which I find very ugly. Is there an easier way?

I'd rather resort to:

(defclass foo ()
  ((x :reader x :initarg x :initform nil)))

and just check if it is nil or not -- in which case x may never be nil (ambiguous).

Paratyphoid answered 2/3, 2012 at 3:28 Comment(3)
It's been a long time since I've used CLOS, but I think you need slot-boundp.Shawnee
What do you provide as second argument?Paratyphoid
Check the common-lisp manual: lispworks.com/documentation/HyperSpec/FrontShawnee
S
16

search for all symbols with SLOT in package CL:

CL-USER 1 > (apropos "SLOT" "CL")

SLOT-MISSING (defined)
UNBOUND-SLOT-INSTANCE (defined)
SLOT-VALUE (defined)
SLOT-BOUNDP (defined)
SLOT-EXISTS-P (defined)
WITH-SLOTS (defined macro)
SLOT-MAKUNBOUND (defined)
UNBOUND-SLOT
MAKE-LOAD-FORM-SAVING-SLOTS (defined)
SLOT-UNBOUND (defined)

I would guess that SLOT-BOUNDP does what you want. By looking at the Common Lisp HyperSpec we can verify this:

Snub answered 2/3, 2012 at 9:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.