24

It is an easy to answer question (I guess), but I looked for a while not finding anything so I will direct my question to you.

There is the typep to determine whether a given variable is of some specific data-type e.g. integer,hashtable etc. , but is there a function which returns the data-type?

e.g.

(defvar *x* 1)
*x*

(typep *x* 'integer)
T

(the-type-function *x*)
INTEGER
2
  • 1
    Though, to nitpick, the type is actually attached to the object (stored in a variable), not to the variable.
    – Dirk
    Jun 5, 2012 at 19:20
  • Rainer Joswigs answer fit my needs, as I had a Class I could not identify but needed its type to check for it before calling some functions on it.
    – Sim
    Jun 7, 2012 at 11:39

1 Answer 1

41

There is the typep to determine whether a given variable is of some specific data-type e.g. integer,hashtable etc. ,

Not really. In Common Lisp variables are not typed as you think.

(defvar *x* 1)
*x*

(typep *x* 'integer)
T

Above says nothing about the type of a variable *x*. It confirms that the object 1 is of type integer.

but is there a function which returns the data-type?

Not really. There is a function TYPE-OF, which returns the type of an object, not of a variable.

> (type-of 1)
FIXNUM

There is no difference when we get the value from a variable.

> (type-of *x*)
FIXNUM

But that does not mean the variable has that type.

Note: Common Lisp has types and type declarations. But that looks slightly different.

1
  • 1
    Thanks, type-of is the perfect answer to my question.
    – Yu Shen
    Jan 12, 2019 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.