如果我有两个并排定义的接口(interface),并且我希望在契约(Contract)内的另一个接口(interface)中引用每个接口(interface),即:
(define context-interface<%>
(interface ()
[entity-list (->m (listof (is-a?/c entity-interface<%>)))]
)
)
(define entity-interface<%>
(interface ()
[on-add (->m (is-a?/c context-interface<%>) void?)]
)
我应该怎样做才能避免在定义之前无法引用标识符
错误?我在 Racket 文档中没有找到类似 C++ 中的前向声明的含义。来自 answer对于我之前的问题,我知道可以使用lazy-require
以某种方式解决问题,但是如果我想将两个定义保留在同一个源文件中怎么办?
最佳答案
您可以通过添加 recursive-contract
来修复递归合约中的此类“值未初始化”错误围绕契约(Contract)的一部分,推迟该部分的评估,直到需要为止。在这种情况下,您可以添加 recursive-contract
左右(is-a?/c entity-interface<%>)
.
(define context-interface<%>
(interface ()
[entity-list (->m (listof (recursive-contract (is-a?/c entity-interface<%>))))]
))
(define entity-interface<%>
(interface ()
[on-add (->m (is-a?/c context-interface<%>) void?)]
))
请注意,它可以在 (is-a?/c entity-interface<%>)
左右因为这是契约(Contract)值(value),但它不能只是entity-interface<%>
左右因为这是 Racket /类接口(interface)值,而不是契约(Contract)。
关于interface - Racket:接口(interface)的前向声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59823776/