TclOO 对象可以在解释器之间 shared/aliases 吗?
Can TclOO objects be shared/aliases between interpreters?
我正在实施 DSL 处理。我正在使用安全解释器 source
输入文件。
作为处理的一部分,我正在构建一个对象。
类似于:
set interp [interp create -safe]
interp expose $interp source
interp eval $interp {
oo::class create Graph { # ...
}
# add domain-specific commands here
proc graph {definition} {
global graph
$graph add $definition
}
# and so on
set graph [Graph new]
}
interp eval $interp source $graphFile
是否有一种机制可以将 $graph
对象作为主解释器的别名?
我得到了什么:
oo::class create Graph { # ...
}
set graph [Graph new]
set interp [interp create -safe]
interp expose $interp source
interp alias $interp graphObj {} $graph
interp eval $interp {
# add domain-specific commands here
proc graph {definition} {
graphObj add $definition
}
# and so on
}
interp eval $interp source $graphFile
puts [$graph someMethod]
别名是命令,不是对象。但是,对于调用(而不是修改定义或创建子类等),您可以设置一个指向其他解释器中的对象的别名:
oo::class create Example {
variable count
method bar {} { return "this is [self] in the method bar, call [incr count]" }
}
Example create foo
interp create xyz
interp alias xyz foo {} foo
xyz eval {
puts [foo bar]
}
# this is ::foo in the method bar, call 1
如果您 forward
对跨越解释器边界的别名的方法调用,则单个方法也可以跨越解释器(有一些限制)。这允许各种恶作剧。
oo::class create Example {
variable count
method bar {caller} { return "this is $caller in the method bar, call [incr count]" }
}
Example create foo
interp create xyz
interp alias xyz _magic_cmd_ {} foo bar
interp eval xyz {
oo::class create Example {
forward bar _magic_cmd_ pqr
}
Example create grill
grill bar
}
# this is pqr in the method bar, call 1
我正在实施 DSL 处理。我正在使用安全解释器 source
输入文件。
作为处理的一部分,我正在构建一个对象。
类似于:
set interp [interp create -safe]
interp expose $interp source
interp eval $interp {
oo::class create Graph { # ...
}
# add domain-specific commands here
proc graph {definition} {
global graph
$graph add $definition
}
# and so on
set graph [Graph new]
}
interp eval $interp source $graphFile
是否有一种机制可以将 $graph
对象作为主解释器的别名?
我得到了什么:
oo::class create Graph { # ...
}
set graph [Graph new]
set interp [interp create -safe]
interp expose $interp source
interp alias $interp graphObj {} $graph
interp eval $interp {
# add domain-specific commands here
proc graph {definition} {
graphObj add $definition
}
# and so on
}
interp eval $interp source $graphFile
puts [$graph someMethod]
别名是命令,不是对象。但是,对于调用(而不是修改定义或创建子类等),您可以设置一个指向其他解释器中的对象的别名:
oo::class create Example {
variable count
method bar {} { return "this is [self] in the method bar, call [incr count]" }
}
Example create foo
interp create xyz
interp alias xyz foo {} foo
xyz eval {
puts [foo bar]
}
# this is ::foo in the method bar, call 1
如果您 forward
对跨越解释器边界的别名的方法调用,则单个方法也可以跨越解释器(有一些限制)。这允许各种恶作剧。
oo::class create Example {
variable count
method bar {caller} { return "this is $caller in the method bar, call [incr count]" }
}
Example create foo
interp create xyz
interp alias xyz _magic_cmd_ {} foo bar
interp eval xyz {
oo::class create Example {
forward bar _magic_cmd_ pqr
}
Example create grill
grill bar
}
# this is pqr in the method bar, call 1