How to determine type of variable/value?

Q&A's, tips, howto's
Post Reply
jopython
Posts: 123
Joined: Tue Sep 14, 2010 3:08 pm

How to determine type of variable/value?

Post by jopython »

The newlisp manual has predicates.
But I like to know if there is there a builtin function to find out the type of a symbol/variable/defn which I can use such as
eg:

Code: Select all

(type x) => "list"
(type n) => "integer"
etc...

ryuo
Posts: 43
Joined: Wed May 21, 2014 4:40 pm

Re: How to determine type of variable/value?

Post by ryuo »

There is no built-in function that I know of that can return the type of any newLISP expression. At best, you may be able to construct your own function that uses the existing predicate functions to return a string that is unique for each cell type.

zhanglong
Posts: 9
Joined: Thu Jun 27, 2013 3:58 pm

Re: How to determine type of variable/value?

Post by zhanglong »

Code: Select all

(define types '("nil" "true" "int" "float" "string" "symbol" "context"
    "primitive" "import" "ffi" "quote" "expression" "lambda" "fexpr" "array"
    "dyn_symbol"))

(define (typeof v)
    (types (& 0xf ((dump v) 1))))
the types defined in newlisp.h

jopython
Posts: 123
Joined: Tue Sep 14, 2010 3:08 pm

Re: How to determine type of variable/value?

Post by jopython »

Thanks everyone.

IVShilov
Posts: 19
Joined: Wed Apr 12, 2017 1:58 am

Re: How to determine type of variable/value?

Post by IVShilov »

ryuo wrote:There is no built-in function that I know of that can return the type of any newLISP expression. At best, you may be able to construct your own function that uses the existing predicate functions to return a string that is unique for each cell type.
Despite of

Code: Select all

(define (type x)
  (let (types 
         '("bool" "bool" "integer" "float" 
           "string" "symbol" "context" "primitive" 
           "import-simple" "import-libffi" "quote" "list" "lambda" 
           "fexpr" "array"))
    (types (& 0xf ((dump x) 1)))))
I have tryed to write such function, but find out that

Code: Select all

(lambda? lambda?) -> nil
and number of other interesting paradoxes.

Post Reply