gpt4 book ai didi

dynamic - 如何在Scheme中获取值的类型?

转载 作者:行者123 更新时间:2023-12-03 14:36:21 25 4
gpt4 key购买 nike

我想要一个在运行时获取值类型的函数。使用示例:

(get-type a)

其中 a已被 define d表示为任意的Scheme值。

我该怎么做呢?还是我必须使用cont堆栈的 boolean?number?等自己实现此目的?

最佳答案

在类似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/11566886/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com