如何强制重新加载模块?

时间:2014-07-23 07:56:29

标签: module rebol rebol3

标题说明了一切。在开发模块时,如何强制重新加载,以测试新代码?我可以从模块切换到脚本并引入绑定问题和命名空间冲突,或者我可以在每次修复错误时更改版本。两者都是糟糕的选择。

我正在寻找的是像import/force %my-module.reb那样在运行会话中重新加载模块(现在我必须重新启动R3,这不是非常快的模式使用)。

2 个答案:

答案 0 :(得分:1)

我不知道你是如何导入你的模块的,但如果你将import函数的返回值赋给一个重新执行的变量,那么导入会加载新的代码。

例如,我有文件mod_a.reb

REBOL []

forever [
    b: import %./mod_b.reb
    b/hello
    wait 10
]

和文件mod_b.reb

REBOL []

hello: function [] [
    print "Hello"
]

如果你运行r3 ./mod_a.reb,你会看到每隔10秒打印一次“Hello”字符串。如果在mod_b.reb正在运行时修改mod_a.reb中的字符串,则会看到打印的字符串不同。

答案 1 :(得分:0)

当前模块(出于安全原因)不会覆盖现有值。 它的工作原理是:

>> m: module [name: test][ export my-func: does[print "hello"]]
>> m/my-func
hello
>> my-func ;<- will error because module was not imported yet
** Script error: my-func has no value
>> import m
>> my-func ;<- now `my-func` is in user's but also `lib` contexts
hello
>> lib/my-func
hello

;-- one must unset it in both contexts
;-- and also remove module from list of existing modules

>> remove/part find system/modules 'test 3 unset 'my-func unset in lib 'my-func
>> import module [name: test][ export my-func: does[print "hello again"]]
>> my-func
hello again

可以使用private标志和version将其简化一点:

>> import module [name: test version: 1.0.0 options: [private]][export my-func: does[print "hello"]]
>> lib/my-func ;<- will error, because module is private
** Script error: cannot access my-func in path lib/my-func
>> my-func ;<- but it is still in user's context
hello
>> unset 'my-func
>> import module [name: test version: 1.0.1 options: [private]][export my-func: does[print "hello again"]]
>> my-func
hello again

还可以编写一个用于模块卸载的函数(尽管可能并非在所有情况下都有效)

unload-module: function[module [word!]][
    m: system/modules/:module
    unless m [ exit ]
    e: select spec-of m 'exports
    forall e [
        print ["Unsetting:" e/1]
        try [unset in system/contexts/user e/1]
        try [unset in system/contexts/lib  e/1]
    ]
    remove/part find system/modules module 3
    m
]

; than:

>> import module [name: test][export my-func: does[print "hello"]]
>> my-func
hello
>> unload-module 'test
Unsetting: my-func
>> import module [name: test][export my-func: does[print "hello again"]]
>> my-func
hello again