字符串可以 'late bound' 就好像它们是相同位置的单词一样吗?
Can strings be 'late bound' as if they were words in the same positions?
加载字符串时,默认情况下它会偏向于用户上下文:
>> f: 3
>> outside: object [
f: 4
getter: does [
print reduce compose [(load "f")]
]
]
>> outside/getter
3
原来是LOAD's implementation, which uses intern
到"Import (internalize) words and their values from the lib into the user context."
的神器
为了解决这个问题,我可以避免通过 LOAD 创建可能无用的绑定的低效率,改用 TO-WORD,然后使用对象的 self
重新绑定它:
>> m: 10
>> inside: object [
m: 20
getter: does [
print reduce [(bind to-word "m" self)]
]
]
>> inside/getter
20
现在我的问题是:
考虑到 definitional "scoping" 的工作方式,对于这种模式的 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
无法写入的地方缺少一些根本性的东西?
正如您正确指出的那样,LOAD 偏向于用户上下文。
如果无法访问要将单词(将从 "f" 和 "m" 加载)绑定到的上下文,您的函数确实是不可能的。
幸运的是,上下文是 Rebol 中的 first-class 个对象,因此答案可能如下所示:
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
]
加载字符串时,默认情况下它会偏向于用户上下文:
>> f: 3
>> outside: object [
f: 4
getter: does [
print reduce compose [(load "f")]
]
]
>> outside/getter
3
原来是LOAD's implementation, which uses intern
到"Import (internalize) words and their values from the lib into the user context."
为了解决这个问题,我可以避免通过 LOAD 创建可能无用的绑定的低效率,改用 TO-WORD,然后使用对象的 self
重新绑定它:
>> m: 10
>> inside: object [
m: 20
getter: does [
print reduce [(bind to-word "m" self)]
]
]
>> inside/getter
20
现在我的问题是:
考虑到 definitional "scoping" 的工作方式,对于这种模式的 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
无法写入的地方缺少一些根本性的东西?
正如您正确指出的那样,LOAD 偏向于用户上下文。 如果无法访问要将单词(将从 "f" 和 "m" 加载)绑定到的上下文,您的函数确实是不可能的。 幸运的是,上下文是 Rebol 中的 first-class 个对象,因此答案可能如下所示:
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
]