Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if a symbol is a defstruct name in Common Lisp

Tags:

common-lisp

Is there a simpler way of testing if a symbol is the name of a struct than this:

(fboundp 'make-symbol)
like image 613
Renzo Avatar asked Dec 25 '22 23:12

Renzo


1 Answers

(defun symbol-names-structure-p (symbol)
  (let ((class (find-class symbol nil)))
    (and class (typep class 'structure-class))))

CL-USER 11 > (defstruct foo bar)
FOO

CL-USER 12 > (symbol-names-structure-p 'bar)
NIL

CL-USER 13 > (symbol-names-structure-p 'foo)
T

also:

CL-USER 14 > (ignore-errors (subtypep 'foo 'structure-object))
T
T

CL-USER 15 > (ignore-errors (subtypep 'bar 'structure-object))
NIL
#<CONDITIONS:ILLEGAL-TYPE-SPECIFIER 402001578B>
like image 168
Rainer Joswig Avatar answered Dec 28 '22 23:12

Rainer Joswig