I want a function that gets the type of a value at runtime. Example use:
(get-type a)
where a
has been define
d to be some arbitrary Scheme value.
How do I do this? Or do I have to implement this myself, using a cond stack of boolean?
, number?
etc. ?
To check the type of a variable, you can use the type() function, which takes the variable as an input. Inside this function, you have to pass either the variable name or the value itself. And it will return the variable data type.
A variable is a box-like object that can hold any Scheme value. It is said to be undefined if its box holds a special Scheme value that denotes undefined-ness (which is different from all other Scheme values, including for example #f ); otherwise the variable is defined.
Symbols in Scheme are widely used in three ways: as items of discrete data, as lookup keys for alists and hash tables, and to denote variable references. Looking beyond how they are written, symbols are different from strings in two important respects. The first important difference is uniqueness.
Since Scheme doesn't have a numeric "not equals" operator (like the != operator in C/Java/Python), we have to combine not and = in order to evaluate "not equals".
In Scheme implementations with a Tiny-CLOS-like object system, you can just use class-of
. Here's a sample session in Racket, using Swindle:
$ racket -I swindle Welcome to Racket v5.2.1. -> (class-of 42) #<primitive-class:exact-integer> -> (class-of #t) #<primitive-class:boolean> -> (class-of 'foo) #<primitive-class:symbol> -> (class-of "bar") #<primitive-class:immutable-string>
And similarly with Guile using GOOPS:
scheme@(guile-user)> ,use (oop goops) scheme@(guile-user)> (class-of 42) $1 = #<<class> <integer> 14d6a50> scheme@(guile-user)> (class-of #t) $2 = #<<class> <boolean> 14c0000> scheme@(guile-user)> (class-of 'foo) $3 = #<<class> <symbol> 14d3a50> scheme@(guile-user)> (class-of "bar") $4 = #<<class> <string> 14d3b40>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With