如何在定义模块中使用 Racket 子模块的内容,该子模块使用定义模块中的定义?

问题描述 投票:0回答:1

我需要一个严重污染命名空间的模块,我宁愿不使用

prefix-in
,因为它使得使用它的代码非常可怕,所以我决定尝试将使用这个模块的代码移动到子模块中.

但是,我似乎无法获取该模块中定义的函数,同时获取它们的依赖项in

这是我尝试过的方法,以及我得到的相应错误:

#lang racket
(define foo 3)
(module mod1 racket/base
  (provide bar1)
  (require pollute)
  (define bar1 (+ 1 foo)))
(require 'mod1)
(define (biz1 e) (+ e bar1))

; foo: unbound identifier

第二次尝试:

#lang racket
(define foo 3)
(module+ mod2 #f
  (provide bar2)
  (require pollute)
  (define bar2 (+ 2 foo)))
(define (biz2 e) (+ e bar2))

; bar2: unbound identifier

第三次尝试:

#lang racket
(define foo 3)
(module+ mod3 #f
  (provide bar3)
  (require pollute)
  (define bar3 (+ 3 foo)))
(require 'mod3)
(define (biz3 e) (+ e bar3))

; require: unknown module
;   module name: 'mod3

第四次尝试:

#lang racket
(define foo 3)
(module+ main
  (provide bar4)
  (require pollute)
  (define bar4 (+ 4 foo)))
(define (biz4 e) (+ e bar4))

; bar4: unbound identifier

这可能吗?我做错了一些简单的事情吗?我需要重新使用

prefix-in
吗?

module racket require
1个回答
0
投票

有两种类型的子模块;那些

created with [
module
][2] that can be imported into the containing module with 
(require (submod "." foo))
or
(require 'foo)
 but don't have access to the containing module's environment, and those created with [
module*
][3], which can see the containing module's environment, but whose 
provide`d 标识符无法导出到到包含模块。没有任何类型的子模块是双向的; Racket 不支持循环依赖。

除了

prefix-in

 之外,您还可以研究 
only-in
 以仅从 
pollute
 获取您需要的标识符,而不是所有内容。

© www.soinside.com 2019 - 2024. All rights reserved.