2

文字列をロードすると、デフォルトでユーザー コンテキストに偏ります。

>> f: 3

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

>> outside/getter
3

これは、 「単語とその値を lib からユーザー コンテキストにインポート (内部化)」するために使用するLOAD の実装のアーティファクトであることが判明しました。intern

これを回避するには、LOAD を介して潜在的に役に立たないバインドを作成する非効率性を回避し、代わりに TO-WORD を使用してから、オブジェクトを使用しselfて再バインドします。

>> m: 10

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

>> inside/getter
20

ここで私の質問:定義上の「スコープ」が機能 する方法を考えると、このパターンgetter-codegetter-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 に答える