【问题标题】:How do I use module inside module?如何在模块内使用模块?
【发布时间】:2014-07-08 21:07:19
【问题描述】:

我有这个简单的模块:

REBOL[
    Name: 'test1
    Type: 'module
    Exports: [foo]
]

foo: does [print "foo"]

还有这个:

REBOL[
    Name: 'test2
    Type: 'module
    Exports: [bar]
]

import %test1.reb

foo

bar: does [foo]

当我尝试执行import %test2.reb 时,我收到foo word is not bound to a context 错误。 在这个错误之后,我可以从控制台调用foo,所以它被导入了,但不知何故它对test2 模块是不可见的。那么在模块中使用模块的正确方法是什么?

【问题讨论】:

    标签: module rebol rebol3


    【解决方案1】:

    我不确定这是否是 IMPORT 中的错误,但是使用 NEEDS 标头应该可以工作:

    Rebol [
        Name: 'test2
        Type: 'module
        Exports: [bar]
        Needs: [%test1.reb]
    ]
    
    foo
    
    bar: does [foo]
    

    【讨论】:

    • IMPORT 是一个夹层,可能值得一看源代码,尝试查找在 'test2 中的单词 'foo 未绑定到'test1 中的单词 'foo 现在也应该绑定到 'system/contexts/user 上下文。
    • 或'system/contexts/lib.
    【解决方案2】:

    你可以给import的return设置一个词

    REBOL[
        Name: 'test2
        Type: 'module
        Exports: [bar]
    ]
    
    t1: import %test1.reb
    
    t1/foo
    
    bar: does [t1/foo]
    

    编辑:

    我尝试使用 'do,它成功了,但现在我无法重现它。所以也许还有其他事情发生,或者我确实忘记在重试之前关闭控制台。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-17
    • 2011-05-23
    • 1970-01-01
    • 1970-01-01
    • 2015-12-27
    • 2021-06-26
    相关资源
    最近更新 更多