我想编译这个脚本:
Red [File: %b.red]
context [
a: 123
hidden: 999
set 'global-exports context [
export-a: a
]
]
probe global-exports
但是在尝试使用 $ ./red-13oct19-a4ee537c -r b.red
编译它时出现错误:
*** Red Compiler Internal Error: Script Error : Invalid path value: global-exports
*** Where: register-object
*** Near: [objects/context/global-exports: make object! [
a: none
hidden: none
]]
最佳答案
通常,您可以使用 do [...]
函数包装动态代码,该函数将代码块视为数据,并使用解释器在运行时启动它:
Red [File: %b.red]
context [
a: 123
hidden: 999
do [ ;-- the code inside is run by interpreter at runtime
set 'global-exports context [
export-a: a
]
]
]
probe get 'global-exports ;-- dynamic retrieval
您还必须动态检索值,除非您之前在某处静态初始化它(例如 global-exports: none
)。
在这种特殊情况下,用 make object!
交换 context
就足够了:
Red [File: %b.red]
context [
a: 123
hidden: 999
set 'global-exports make object! [
export-a: a
]
]
probe global-exports
编译动态代码will be possible with JIT compiler ,这是计划在未来进行的,但不会在 Red 版本 1.0 之前。
(感谢@9214和@hiiamboris的想法on red/help gitter chat)
关于dynamic - 如何编译具有动态代码的脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58411199/