如果我创造我自己的类型有两个参数:如何触发缀式显示在REPL
class !:[A, B]
然后创建在REPL一个它的实例没有显示它的类型我想要的方式:
scala> new !:[Int, Int]
res18: !:[Int,Int] = [email protected]
相反,我想它显示是这样的:
scala> new !:[Int, Int]
res18: Int !: Int = [email protected]
这是可能的Scala呢?
如果我创造我自己的类型有两个参数:如何触发缀式显示在REPL
class !:[A, B]
然后创建在REPL一个它的实例没有显示它的类型我想要的方式:
scala> new !:[Int, Int]
res18: !:[Int,Int] = [email protected]
相反,我想它显示是这样的:
scala> new !:[Int, Int]
res18: Int !: Int = [email protected]
这是可能的Scala呢?
缀显示的符号类型是默认在Scala 2.12.2。该更改是在this pull request中进行的。
它还在包scala.annotation
添加注释showAsInfix
给你的,你需要改变默认行为的情况下控制。
不知道这是否有助于但是你可以重写ToString方法和控制输出的第二部分,像这样:
import scala.reflect.runtime.universe.{TypeTag, typeOf}
class A[X, Y] (implicit val tt: TypeTag[X], implicit val ty: TypeTag[Y]) {
override def toString() = typeOf[X] + " !: " + typeOf[Y]
}
new A[Int, Int]()
输出
res15: A[Int,Int] = Int !: Int
感谢您的尝试,但我真的想能够以不同方式显示'A [Int,Int]'。我已经在我的情况下重写了toString方法。 –