球拍:接口的前向声明

博丹·伊凡诺夫(Bohdan Ivanov)

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

(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 ++中的正向声明的内容。上一个问题答案中,我知道可以使用某种方式解决问题lazy-require,但是如果我想将两个定义都保留在同一源文件中怎么办?

亚历克斯·克纳特

您可以在递归合同中添加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?)]
    ))

请注意,它可以存在,(is-a?/c entity-interface<%>)因为这是一个合同价值,但不能仅仅entity-interface<%>因为这是一个球拍/类接口价值,而不是合同,而可以存在。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章