我想要一个在运行时获取值类型的函数。使用示例:
(get-type a)
其中 a
已被定义
为某个任意 Scheme 值。
我该怎么做?还是我必须自己实现,使用 boolean?
、number?
等的 cond 堆栈?
最佳答案
在具有类似 Tiny-CLOS 的对象系统的 Scheme 实现中,您可以只使用 class-of
。这是 Racket 中使用 Swindle 的示例 session :
$ 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>
与使用 GOOPS 的 Guile 类似:
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>
关于dynamic - 如何在 Scheme 中获取值的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18455853/