You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kadena Pact-Lang报错:Cannot define a keyset outside of a namespace

解决Pact REPL中"Cannot define a keyset outside of a namespace"错误

你遇到的错误是因为define-keyset语句被放在了模块外部,而Pact要求这类顶级定义必须处于命名空间(模块)内部,或者在REPL中单独执行。下面提供两种可行的解决方法:

方法1:将keyset定义移到模块内部

修改代码,把define-keyset放到module块的最开头,确保它处于模块的命名空间内:

(module hello 'hello-admin
  "Pact hello-world with database example"
  (define-keyset 'hello-admin (read-keyset 'hello-keyset))

  (defschema hello-schema
    "VALUE stores greeting recipient"
    value:string)

  (deftable hellos:{hello-schema})

  (defun hello (value)
    "Store VALUE to say hello with."
    (write hellos "hello" { 'value: value }))

  (defun greet ()
    "Say hello to stored value."
    (with-read hellos "hello" { "value" := value }
      (format "Hello, {}!" [value])))
)

(create-table hellos)

(hello "world") ;; store "hello"
(greet)         ;; say hello!

方法2:在REPL中单独定义keyset

先在REPL中单独执行以下代码:

(define-keyset 'hello-admin (read-keyset 'hello-keyset))

执行成功后,再粘贴剩余的模块及调用代码执行即可。

错误原因说明

Pact的define-keyset属于需要绑定到命名空间的定义,当你把它和模块代码批量粘贴到REPL时,模块外的define-keyset会被视为在无命名空间的上下文执行,这违反了Pact的语法规则,因此触发错误。

内容的提问来源于stack exchange,提问作者att

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 21:20:32