Racket:接口的前向声明

问题描述 投票:1回答:2

如果我有两个并排定义的接口,并且我想在每个内部合约中引用另一个接口,即:

(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?)]
    )

如何避免cannot reference an identifier before its definition错误?我还没有在Racket文档中找到任何类似C ++中的正向声明的内容。从上一个问题的answer中,我知道可以使用lazy-require以某种方式解决问题,但是如果我想将两个定义都保留在同一源文件中怎么办?

interface scheme racket
2个回答
1
投票

您可以通过在合同的一部分周围添加recursive-contract来推迟对递归合同的这种“未初始化的值”错误,以将该部分的评估推迟到需要时再进行。在这种情况下,可以在recursive-contract周围添加recursive-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?)] )) 左右,因为这是一个球拍/类接口价值,而不是合同。


0
投票

完成这项工作:

(is-a?/c entity-interface<%>)
© www.soinside.com 2019 - 2024. All rights reserved.