测试符号是否是宏中的散列-table

Testing if a symbol is a hash-table in a macro

我想构建一个宏,根据作为参数提供的符号类型扩展为不同的形式。一个可重现的小例子(其中两个,实际上......在 sbcl 和 ccl 上失败)如下:

λ (defmacro what-am-i (a-thing)
   (etypecase a-thing
        (list         `(format t "im a list"))
        (vector       `(format t "im a vector"))
        (hash-table   `(format t "im a hash-table"))))

λ (defmacro what-am-i2 (a-thing)
     (cond
        ((typep a-thing 'list)         `(format t "im a list"))
        ((typep a-thing 'vector)       `(format t "im a vector"))
        ((typep a-thing 'hash-table)   `(format t "im a hash-table"))))

λ (what-am-i '(1 2 3 4))
im a list
NIL

λ (what-am-i "abcd")
im a vector
NIL

λ (what-am-i *my-hash*)

debugger invoked on a SB-KERNEL:CASE-FAILURE in thread
#<THREAD "main thread" RUNNING {10039846F3}>:
  *MY-HASH* fell through ETYPECASE expression.
  Wanted one of (LIST VECTOR HASH-TABLE).

Type HELP for debugger help, or (SB-EXT:EXIT) to exit from SBCL.

restarts (invokable by number or by possibly-abbreviated name):
  0: [ABORT] Exit debugger, returning to top level.

(SB-KERNEL:CASE-FAILURE ETYPECASE *MY-HASH* (LIST VECTOR HASH-TABLE))
0] ^D

λ (what-am-i2 '(1 2 3 4))
im a list
NIL

λ (what-am-i2 "abcd")
im a vector
NIL

λ (what-am-i2 *my-hash*)

NIL
λ (typep *my-hash* 'hash-table)

T

谁能告诉我我做错了什么?

交易品种的类型总是symbol :-)

在代码执行(评估)之前,符号的的类型是未知的。

所以,(what-am-i '(1 2 3 4))看到一个列表(quote (1 2 3 4)),报错是一个列表,而(what-am-i *my-hash*)看到一个符号*my-hash*,报错

您可以使用symbol-value在宏展开时访问符号的全局值,但我怀疑您在这里问错了。

我建议你问一个单独的问题,解释你实际想要完成的事情。