Access to "parent scope" in JShell

306 views Asked by At

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

There are 1 answers

2
GhostCat On BEST ANSWER

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 )