Namespaces Racket:eval、名称空间附加模块与名称空间要求
假设我有一个模块“foo.rkt”,它导出一个结构foo,例如Namespaces Racket:eval、名称空间附加模块与名称空间要求,namespaces,racket,eval,Namespaces,Racket,Eval,假设我有一个模块“foo.rkt”,它导出一个结构foo,例如 #lang racket (provide foo) (struct foo ()) 在另一个模块中,我使用“foo.rkt”,但我也希望将“struct foo”的绑定与另一个名称空间相关联(出于各种原因,我不使用预设,因此我不能使用名称空间) 我认为我可以使用名称空间附加模块,如下所示: (define ns (make-base-namespace)) (namespace-attach-module (current-na
#lang racket (provide foo) (struct foo ())
在另一个模块中,我使用“foo.rkt”,但我也希望将“struct foo”的绑定与另一个名称空间相关联(出于各种原因,我不使用预设,因此我不能使用名称空间)
我认为我可以使用名称空间附加模块,如下所示:
(define ns (make-base-namespace))
(namespace-attach-module (current-namespace) "foo.rkt" ns)
(eval '(foo) ns)
但这不起作用,因为名称空间映射符号显示s没有绑定到ns中(如果这是查找绑定的唯一位置)。但是,它在REPL中确实起作用。为什么?我认为问题在于避免在“foo.rkt”中实例化模块两次,因为这会导致两个不兼容的结构定义 函数
名称空间附加模块
是拼图的一部分,但它只附加
名称空间ns的实例化模块(即名称“foo.rkt”)现在与“foo.rkt”的正确实例化相关联。但是,它不能使绑定在ns中可用-这是名称空间所需的工作
以下是一个例子:
文件:“computer.rkt”
文件:“使用computer.rkt”
运行此操作的结果是:
"Apple"
请注意,删除名称空间附加模块
行会导致以下错误:
computer-name: contract violation;
given value instantiates a different structure type with the same name
expected: computer?
given: (computer "Apple" 2000)
由于没有附件,名称空间require
将再次启动“computer.rkt”,导致两个不兼容的结构begin声明。非常感谢,这就是我要找的。
"Apple"
computer-name: contract violation;
given value instantiates a different structure type with the same name
expected: computer?
given: (computer "Apple" 2000)