How to use members with standard (batch) or private access in REPL?

I tried to add some interactivity to my testing / debugging cycle, so I tried playing around with my classes from Scala REPL. This works fine, but it has the disadvantage that I cannot access the members of the package and the private level, which can be tested with unit test (if the test is in the same package).

Can I set the "context" of a Scala REPL package?

I suppose I could use reflection to access members, but typing it so much will in the first place exceed the goal of using REPL.

+6
source share
2 answers

I assume that the class you are testing is written in Java, since you need to get out of the way to create only a package member in Scala.

In short, this is not possible. Each line in the REPL is wrapped in its own package, so it will not be allowed access to another member only for packages from any other package. Despite the lack of an undocumented system property for changing the default package name prefix used for packaging, the package name is still generated automatically, increasing the number:

$ scala -Xprint:parser -Dscala.repl.naming.line=foo.line scala> val x = 1 [[syntax trees at end of parser]]// Scala source: <console> package foo.line1 { object $read extends scala.ScalaObject { // snip ... object $iw extends scala.ScalaObject { // snip ... object $iw extends scala.ScalaObject { // snip ... val x = 1 } } } 

Assuming you do this often, you can create a file that makes it easier to use reflection, and then load it into the REPL with the command :load .

+5
source

You mean that you cannot access the elements defined in the package object ? You can import these items using

 import mypackage._ 

or just access them using the mypackage.mymember(...) prefix form.

+1
source

Source: https://habr.com/ru/post/898822/


All Articles