2

当您加载字符串时,默认情况下它会偏向于用户上下文:

>> f: 3

>> outside: object [
    f: 4
    getter: does [
        print reduce compose [(load "f")]
    ]
]

>> outside/getter
3

事实证明这是LOAD 实现的工件,它用于intern“将单词及其值从 lib 导入(内部化)到用户上下文中”。

为了解决这个问题,我可以避免通过 LOAD 创建潜在无用绑定的低效率,改用 TO-WORD,然后使用对象self重新绑定它:

>> m: 10

>> inside: object [
    m: 20
    getter: does [
        print reduce [(bind to-word "m" self)]
    ]
]

>> inside/getter
20

现在我的问题是: 鉴于定义性“范围界定”getter-code的工作方式,对于这种模式和getter-text两种输出来说,根本就没有办法——现在或未来4 20

>> f: 0

>> m: 0

>> ctx: context [
    f: 4

    m: 10

    both: object [
        m: 20
        getter-code: does [print [f m]]
        getter-text: does [impossible-print ["f" "m"]]
    ]
]

例如,从根本上缺少一些impossible-print无法写入的地方?

4

1 回答 1

2

正如您正确指出的那样, LOAD 偏向于用户上下文。如果无法访问您想要将单词(将从“f”和“m”加载)绑定到的上下文,那么您的函数确实是不可能的。幸运的是,上下文是 Rebol 中的一等对象,因此答案可能如下所示:

f: m: 0 ctx: context [
    f: 4
    m: 10
    both: object [
        m: 20
        getter-code: does [print [f m]]
        getter-text: does [impossible-print reduce ["f" ctx "m" self]]
    ]
]
impossible-print: func [vars /local r][
    r: copy []
    foreach [v c] vars [append r bind to-word v c]
    print r
]
于 2015-08-02T23:53:40.270 回答