2016-08-29 21 views
3

當在打字球拍中定義結構體時,我不能再使用prop:procedure。在正常球拍我可以這樣做:無法應用道具:打字球拍中的程序

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

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

但是,當我嘗試在類型化/球拍,我得到一個類型檢查錯誤:

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

是否有輸入球拍定義這個屬性的另一種方式?

+0

不再像在'#lang typed/racket'中工作過嗎? – Sylwester

+0

'thunk *'現在也不會在類型化的球拍中進行類型檢測 –

回答

3

正如@Leif Andersen所說,#:property結構選項在打字球拍中不起作用。

但是對於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 
+1

哦,那很好,謝謝分享。 –

2

typed/racket中的結構不能有任何#:property字段。他們也不支持泛型。

事實上,你甚至可以這樣稱呼它看起來像一個bug。

如果你確實想雖然調用像功能的結構,你可以通過定義它非類型化代碼,使用require/typed#:struct得到它爲鍵入的代碼,並使用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被稱爲過程。