Access to “parent scope” in JShell

本小妞迷上赌 提交于 2019-12-07 01:26:37

问题


It seems, that JShell object created inside another JShell does not have access to parent's JShell scope. For instance:

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)]

Is it somehow possible to make parent scope visible to the child one?


回答1:


According to this the one big caveat about JShell is: it runs in its own JVM.

The javadoc for create() says:

Equivalent to JShell.builder().build().

And when you follow to the javadoc for build(), you find:

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.

In other words: most likely, you are creating another JVM instance where that other shell runs. So at least for now: no chances of having a child jshell know about its parent.

( as in: I seriously hope that this REPL feature of Java will allow at some future point to attach a JShell to an already running JVM )



来源:https://stackoverflow.com/questions/46214641/access-to-parent-scope-in-jshell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!