无法在类型球拍中应用 prop:procedure

Can not apply prop:procedure in typed racket

在 typed racket 中定义结构时,我不能再使用 prop:procedure。在普通球拍中,我可以做类似的事情:

(struct profile-unit (a t c g)
    #:property prop:procedure (thunk* 12))

(define t (profile-unit .1 .1 .2 .6))
(t)
> 12

但是当我在 typed/racket 中尝试时,出现类型检查错误:

(struct profile-unit ([a : Real] [t : Real] [c : Real] [g : Real])
  #:property prop:procedure (thunk* 12))
(t)
> Type Checker: Cannot apply expression of type profile-unit, since it is not a function type in: (t)

在打字球拍中是否有另一种定义此 属性 的方法?

typed/racket 中的结构不能有任何 #:property 字段。他们也不支持泛型。

事实上你甚至可以这样称呼它,这对我来说就像是一个错误。

如果您真的想像调用函数一样调用结构,可以通过在无类型代码中定义它来实现,使用 require/typed an #:struct to get it into typed code, and using cast 将其转换为过程。例如:

#lang typed/racket

(module foo racket
  (provide (struct-out profile-unit)
           make-profile-unit)

  (struct profile-unit (a t c g)
    #:property prop:procedure (thunk* 12))
  (define make-profile-unit profile-unit)
  ((profile-unit 1 2 3 4)))

(require/typed 'foo
               [#:struct profile-unit ([a : Real]
                                       [t : Real]
                                       [c : Real]
                                       [g : Real])])

((cast (profile-unit 1 2 3 4) (-> Any)))

在此示例中,profile-unit 被作为过程调用。

正如@Leif Andersen 所说,#:property struct 选项在 typed racket 中不起作用。

但是对于 prop:procedure 的特殊情况,您可以使用 define-struct/exec 形式。

#lang typed/racket

(define-struct/exec profile-unit ([a : Real] [t : Real] [c : Real] [g : Real])
  [(λ (this) 12) : (profile-unit -> Any)])

(define t (profile-unit .1 .1 .2 .6))
(t) ; 12