球拍 - 当有合同时,不能通过场地通道发送预制结构

时间:2017-05-31 02:27:57

标签: scheme racket

我遇到了一个问题,如果结构是通过合同导入的,我就无法通过地方通道发送预制结构。这是一个完整的例子:

#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"))))

使用Racket 6.8,可以打印:

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

提及合同的documentation中没有任何内容。这是一个实际限制,如果是这样,有什么方法可以解决它吗? (我想,只是创建另一个通过频道发送的结构)。

1 个答案:

答案 0 :(得分:2)

错误....因为这是一个预制结构(并且你希望它将它发送到一个地方),你的所有数据都是平的,所以你可以轻松地手工制作预制结构。例如:

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

有点奇怪,但是,嘿,你仍然可以做这个并且有你的支票(因为它们仍然是平的。)事实上,你甚至可以定义一个函数让它为你序列化:

(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