看来,在另一个 JShell
中创建的 JShell
对象无法访问父级的 JShell
范围。例如:
jshell> int x = 1;
x ==> 1
jshell> x
x ==> 1
jshell> jdk.jshell.JShell js = jdk.jshell.JShell.create();
js ==> jdk.jshell.JShell@1a052a00
jshell> js.eval("x");
$4 ==> [SnippetEvent(snippet=Snippet:ErroneousKey#1-x,previousStatus=NONEXISTENT,status=REJECTED,isSignatureChange=false,causeSnippetnull)]
jshell> js.eval("int x = 2;");
$5 ==> [SnippetEvent(snippet=Snippet:VariableKey(x)#2-int x = 2;,previousStatus=NONEXISTENT,status=VALID,isSignatureChange=true,causeSnippetnullvalue=2)]
jshell> js.eval("x");
$6 ==> [SnippetEvent(snippet=Snippet:ExpressionKey(x)#3-x,previousStatus=NONEXISTENT,status=VALID,isSignatureChange=true,causeSnippetnullvalue=2)]
是否有可能使父范围对子范围可见?
最佳答案
根据 this关于 JShell 的一个重要警告是:它在其自己的 JVM 中运行。
create() 的 javadoc说:
Equivalent to JShell.builder().build().
当您关注 build() 的 javadoc 时,你会发现:
Build a JShell state engine. This is the entry-point to all JShell functionality. This creates a remote process for execution. It is thus important to close the returned instance.
换句话说:您很可能正在创建运行其他 shell 的另一个 JVM 实例。所以至少现在:没有机会让 child jshell 知道它的父级。
(如:我真心希望 Java 的这个 REPL 特性在未来某个时候允许将 JShell 附加到已经运行的 JVM )
关于java - 在 JShell 中访问 "parent scope",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46214641/