首页 文章

在sbt console / Scala REPL中抑制返回类型

提问于
浏览
6

我记得在某个地方有切换来抑制Scala REPL中返回类型的打印,但我找不到它 . 我特别感兴趣的是将此开关添加到sbt构建文件中 . 像 returnTypes in console := false 之类的东西 .

例如 . 我现在有

scala> within( Span( 0, 33 ))
res7: scala.collection.immutable.IndexedSeq[(de.sciss.lucre.expr.SpanLike, scala.collection.immutable.IndexedSeq[(de.sciss.lucre.expr.Expr[de.sciss.lucre.stm.InMemory,de.sciss.lucre.expr.SpanLike], de.sciss.lucre.expr.Expr[de.sciss.lucre.stm.InMemory,Long])])] = Vector()

而且出于明显的原因,我想要

scala> within( Span( 0, 33 ))
res7: Vector()

1 回答

  • 5

    我的问题基本上是由this mailing-list question反映出来的 . 根据Rex Kerr的想法,以下内容可以进入 build.sbt

    initialCommands in console := """// helper method to disable type printing
    def shortresults[T](t: => T) = {
       val s = t.toString
       val name = s.takeWhile(_ != ':')
       val idx = s.indexOf(" = ")
       val full = if (idx >= 0) name + s.substring(idx) else s
       val short = if (full.length>799) full.substring(0,796)+"..." else full
       print(short)
       t
    }
    """
    

    但遗憾的是,在控制台启动并运行后,仍需要手动执行以下三个REPL转义命令:

    :power
    :wrap shortresults
    :silent
    

相关问题