Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine the datatype of a variable

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
like image 768
Sim Avatar asked Jun 05 '12 15:06

Sim


People also ask

How can you know the datatype of a variable?

To check the type of any variable data type, we can use the type() function. It will return the type of the mentioned variable data type. Float data type is used to represent decimal point values.

How do you determine the type of a variable in Python?

To get the type of a variable in Python, you can use the built-in type() function. In Python, everything is an object. So, when you use the type() function to print the type of the value stored in a variable to the console, it returns the class type of the object.

How do you determine the data type of an object?

To determine the data type an Object variable currently refers to, you can use the GetTypeCode method of the System. Type class. The following example illustrates this. The Object data type is a reference type.


1 Answers

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.

like image 80
Rainer Joswig Avatar answered Sep 25 '22 18:09

Rainer Joswig