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

时间:2012-06-16 19:10:40

标签: scala sbt read-eval-print-loop

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

E.g。现在我有

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 个答案:

答案 0 :(得分: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