Mikaël Mayer
Mikaël Mayer

Reputation: 10701

How to trigger infix type display in REPL

If I create my own type with two arguments:

class !:[A, B]

then creating an instance of it in the REPL does not display its type the way I want:

scala> new !:[Int, Int]
res18: !:[Int,Int] = $bang$colon@5eb1479

Instead, I would like it to display like this:

scala> new !:[Int, Int]
res18: Int !: Int = $bang$colon@5eb1479

Is this possible in scala ?

Upvotes: 0

Views: 54

Answers (2)

Oleg Pyzhcov
Oleg Pyzhcov

Reputation: 7353

Infix display for symbolic types is default in Scala 2.12.2. The change was made in this pull request.

It also added annotation showAsInfix in package scala.annotation to give you control in cases you need to alter default behavior.

Upvotes: 1

jsdeveloper
jsdeveloper

Reputation: 4045

Not sure if this helps but you could override the toString method and control the second part of the output, like this:

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]()

Outputs

res15: A[Int,Int] = Int !: Int

Upvotes: 0

Related Questions