Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print out Scala worksheet results in interactive mode in IntelliJ

For some reason, intermediate values aren't being printed out in the REPL console (right hand side of the worksheet)

For instance, this is what I have:

object test {
  val obj = new MyObject(1)
  obj.value 
}

class MyObject(x: Int) {
  def value = x
}

In the REPL results, I only get the following:

defined module test
.
.
.
defined class MyObject

However, I don't get any of the intermediate results, such as when I evaluate x.value

I would expect something like:

> MyObject@14254345
> 1 

after x.value

Any reason why this isn't printing out?

like image 619
reectrix Avatar asked Jul 22 '15 14:07

reectrix


People also ask

How do I create a Scala worksheet in IntelliJ?

Create an .Right-click your project and select New|Scala Worksheet. We recommend that you create an . sc file in the src directory to avoid problems with code highlighting. In the New Scala Worksheet window, type the name of the file and click OK.

How do I use worksheets in Scala?

A worksheet is a scala file with . sc extension which you can run and get evaluation results in a special view appeared in the editor. Create worksheet by right-clicking on your Project and choosing 'New' -> 'Scala Worksheet'. Just type your code, press 'Evaluate worksheet' button and results appear.


3 Answers

What ended up working for me in this case (and this might be particular to IntelliJ 14, since I've seen it working the other way in Eclipse) is I added the class inside the object block, like this:

object test {
  val obj = new MyObject(1)
  obj.value 

  class MyObject(x: Int) {
   def value = x
  }
}

This forced the REPL instance inside the worksheet to auto-evalute the result and print them out on the right hand side.

like image 79
reectrix Avatar answered Sep 18 '22 14:09

reectrix


To make it work like it does in Eclipse, turn on "eclipse compatibility" mode. This worked for me using IntelliJ IDEA 2016.

Preferences > Language & Frameworks > Scala > Worksheet

Then, check the Use "eclipse compatibility" mode checkbox.

like image 35
sjking Avatar answered Sep 20 '22 14:09

sjking


Sorry, I don't have enough reputation to comment so I have to write it here.

If you want to get the result you want, maybe you can try like this.

scala> :paste
// Entering paste mode (ctrl-D to finish)

object test {
  val obj = new MyObject(1)
  println(obj.value) 
}

class MyObject(x: Int) {
  def value = x
}


// Exiting paste mode, now interpreting.

defined object test
defined class MyObject

scala> test.obj
1
res4: MyObject = MyObject@1cd072a9

when you paste the code, test and MyObject are not initialized, certainly you can not get any print. test.obj will cause test to be initialized so will obj, meantime, obj.value also get evaluated. However, if you don't something about it(like print), it's just a pure expression.

like image 25
Allen Chou Avatar answered Sep 20 '22 14:09

Allen Chou