gpt4 book ai didi

module - 在当前命名空间中声明一个模块用于 `module->exports`

转载 作者:行者123 更新时间:2023-12-04 16:51:25 28 4
gpt4 key购买 nike

我想使用功能module->exports查找模块提供的所有导出。不幸的是,传递给该函数的模块必须在当前命名空间中声明,然后才能在其上使用该函数。当我静态地知道模块是什么时,这没问题,我只需要将其引入当前模块,然后使用该功能。

但是,如果我不知道静态模块,并传入一个模块路径索引(我知道它指向文件系统中的有效模块),我似乎无法将模块声明到当前命名空间中。假设我想制作一个 module-path-index->​​exports 函数:

(define (module-path-index->exports mpi)
(parameterize ([current-namespace (make-base-namespace)])
<stuff to load module path index into current-namespace>
(if (module-declared? mpi)
(module->exports mpi)
(error "Module not declared"))))

关于如何声明模块的任何建议?

更新:

我不能只用 namespace-reaquire在这里,因为它似乎失败了 module-path-index s 来自 identifier-binding功能。例如,以下代码:
#lang racket

(define binding (identifier-binding #'contract-continuation-mark-key))

(parameterize ([current-namespace (make-base-namespace)])
(dynamic-require (first binding) (void))
(module->exports (first binding)))

给出这个错误:
Language: racket, with debugging; memory limit: 8192 MB.
. . require: unknown module
module name: #<resolved-module-path:"/Users/leif/racket/racket/collects/racket/contract/private/guts.rkt">

最佳答案

关闭!但是需要解析模块路径索引(first binding) .执行此操作的函数是 resolve-module-path-index来自 syntax/modresolve .

#lang racket

(require syntax/modresolve)

(define binding (identifier-binding #'contract-continuation-mark-key))

(parameterize ([current-namespace (make-base-namespace)])
(define resolved (resolve-module-path-index (first binding) (current-directory)))
(dynamic-require resolved (void))
(module->exports resolved))

关于module - 在当前命名空间中声明一个模块用于 `module->exports`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37313906/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com