[英]How to pretty print a scala type?
我想向用户显示一个值,一个简化类型和原始类型。
我正在使用pprint库作为值和scala.Manifest
作为类型。
def render[T](a: T)(implicit m: Manifest[T]): (String, String, String) =
(pprint(a), simplify(m), m.toString)
我想实现这个simplify
功能。
它将删除普通进口
def simplify[T](m: Manifest[T]): String =
m.toString
.replaceAll("scala.", "")
.replaceAll("scala.collection.immutable.", "")
.replaceAll("scala.collection.mutable.", "")
.replaceAll("java.lang.", "")
但是m.toString
具有以下缺点
(Int, Int) => Int
成为scala.Function2[Int, Int, Int]
(Int, Int)
成为scala.Tuple2[Int, Int]
使用TPrint[T]
在TPrint[T]
-repl中实现漂亮的打印类型
libraryDependencies += "com.lihaoyi" % "ammonite-repl" % "0.5.4" cross CrossVersion.full
import ammonite.repl.frontend.TPrint
val config = pprint.Config.Defaults.PPrintConfig
def render[T](a: T)(implicit tp: TPrint[T], m: Manifest[T]): (String, String, String) =
(pprint(a), tp.render(config), m.toString)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.