Print the entire result in the Scala interactive console

When I enter something into the Scala interactive console, the console prints the result of the instruction. If the result is too long, the console trims it (scroll right to see it):

scala> Array.fill[Byte](5)(0) res1: Array[Byte] = Array(0, 0, 0, 0, 0) scala> Array.fill[Byte](500)(0) res2: Array[Byte] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... scala> "a"*5000 res3: String = ... 

How can I print the same or equivalent output for any given object (and not just a collection or an array) without cropping?

+6
source share
3 answers

The result is not cropped, just println calls java.lang.Arrays.toString() (since scala.Array is a Java array).

In particular, Arrays defines an overload of toString that works with Object , which calls the toString implementation of java.lang.Object for each element. This implementation prints a reference to the object, so you get

 [Lscala.Tuple2;@4de71ca9 

which is an Array containing the reference 4de71ca9 to the scala.Tuple2 object.

This was discussed on this ticket years ago.


In the specific case of arrays you can simply do

 println(x.mkString("\n")) 

or

 x foreach println 

or

 println(x.deep) 

Update

To answer the last change, you can set the maximum length of lines printed by REPL

 scala> :power ** Power User mode enabled - BEEP WHIR GYVE ** ** :phase has been set to 'typer'. ** ** scala.tools.nsc._ has been imported ** ** global._, definitions._ also imported ** ** Try :help, :vals, power.<tab> ** scala> vals.isettings.maxPrintString = Int.MaxValue vals.isettings.maxPrintString: Int = 2147483647 
+17
source

to try

 x map println 

or

 x foreach println 
+1
source

try it

 scala> :power Power mode enabled. :phase is at typer. import scala.tools.nsc._, intp.global._, definitions._ Try :help or completions for vals._ and power._ scala> vals.isettings.maxPrintString res9: Int = 800 scala> vals.isettings.maxPrintString = 10000 vals.isettings.maxPrintString: Int = 10000 
0
source

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


All Articles