Print whole result in interactive Scala console

后端 未结 3 897
走了就别回头了
走了就别回头了 2021-02-02 18:19

When I type something into the Scala interactive console, the console prints the result of the statement. If the result is too long, the console crops it (scroll right to see i

相关标签:
3条回答
  • 2021-02-02 18:36

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

    Specifically, Arrays defines a toString overload that works with Object, which calls the toString implementation of java.lang.Object on every element. Such implementation prints the reference of the object, so you end up with

    [Lscala.Tuple2;@4de71ca9
    

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

    That has been discussed in 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 your last edit, you can set the maximum lenght of the strings printed by the 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
    
    0 讨论(0)
  • 2021-02-02 18:56

    try this

    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 讨论(0)
  • 2021-02-02 18:58

    try

    x map println
    

    or

    x foreach println
    
    0 讨论(0)
提交回复
热议问题