2017-05-31 36 views
2

我遇到了一個問題,如果結構是用合同導入的,我無法在場所通道中發送預製結構。這裏有一個完整的例子:球拍 - 無法通過位置通道發送預製結構當它有一個合同

#lang racket 

(module structs racket 

    (provide 
    example-without-contract 
    (contract-out [struct example-with-contract ([thing string?])])) 

    (struct example-without-contract (thing) #:prefab) 
    (struct example-with-contract (thing) #:prefab)) 

(require 'structs) 
(displayln (format "Can send example-without-contract: ~A" 
        (place-message-allowed? (example-without-contract "abc")))) 
(displayln (format "Can send example-with-contract: ~A" 
        (place-message-allowed? (example-with-contract "abc")))) 

球拍6.8,這個打印:

Can send example-without-contract: #t 
Can send example-with-contract: #f 

沒有什麼在提到合同documentation。這是一個實際的限制,如果是這樣,有什麼辦法可以解決它嗎? (我猜想只是創建另一個通過頻道發送的結構)。

+0

由於合同本身無法通過地方渠道發送,這是有道理的,我認爲有合同結構附屬於它也無法通過地點頻道發送。我不相信有辦法解決這個問題。 –

回答

2

Err ....因爲這是一個預製結構(並且您希望將它發送到一個地方),所有數據都是平坦的,所以您可以輕鬆地手動製作預製結構。例如:

> (define s '#s(example-with-contract "abc")) 
> (example-with-contract? s) 
#t 
> (displayln (format "Can send example-with-contract: ~A" 
       (place-message-allowed? s))) 
Can send example-with-contract: #t 

Kindof奇怪,但是,嘿,你仍然可以做這個,有你的支票事實上,你甚至可以定義一個函數,使之序列化你(因爲再次,他們仍然是平的。) :

(require racket/struct) 
(define (serialize-prefab s) 
    (define key (prefab-struct-key s)) 
    (define elems (struct->list s)) 
    (apply make-prefab-struct key elems)) 

而現在,你可以在一個地方消息發送您的預製結構,

> (displayln (format "Can send example-with-contract: ~A" 
       (place-message-allowed? 
       (serialize-prefab (example-with-contract "abc"))))) 
Can send example-without-contract: #t 
+0

這與我最終做的類似,謝謝。 –