2017-05-31 10 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

这与我最终做的类似,谢谢。 –