Skip to content

Commit

Permalink
Add support for Polytypes (#79)
Browse files Browse the repository at this point in the history
* Add PolyType to TPrintImpl

* Add TPrint test for polytype

* Tweak based on PR feedback

* Allow Polytypes to be different on 2.12 vs 2.13
  • Loading branch information
valencik authored Mar 27, 2022
1 parent f24db51 commit 2626e05
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pprint/src-2/TPrintImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ object TPrintLowPri{
.map(typePrintImplRec(c)(_, true))
.reduceLeft[fansi.Str]((l, r) => l ++ " with " ++ r)
(pre + (if (defs.isEmpty) "" else "{" ++ defs.mkString(";") ++ "}"), WrapType.NoWrap)
case PolyType(typeParams, resultType) =>
val params = printArgSyms(typeParams)
(
params ++ typePrintImplRec(c)(resultType, true),
WrapType.NoWrap
)
case ConstantType(value) =>
val pprintedValue =
pprint.PPrinter.BlackWhite.copy(colorLiteral = fansi.Color.Green).apply(value.value)
Expand Down
16 changes: 16 additions & 0 deletions pprint/test/src-2/test/pprint/TPrintTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,22 @@ object TPrintTests extends TestSuite{
import scala.reflect.runtime.universe._
check[Nothing]("Nothing")
}
test("polytype"){
import scala.language.implicitConversions
type Foo[+A] = Unit
trait Bar[F[_], A]
object Bar {
implicit def anyToBar[A](a: A): Bar[Foo, A] = new Bar[Foo, A] {}
def apply[F[_], A](b: Bar[F, A]): Bar[F, A] = b
}
object Print {
def tprint[A](a: A)(implicit t: pprint.TPrint[A]) = t.render
}
val rendered = Print.tprint(Bar(1)).toString()

val is213Plus = classOf[Seq[Int]].getName != "scala.collection.Seq"
assert(rendered == (if (is213Plus) "Bar[[A]Foo[A], Int]" else "Bar[Foo, Int]"))
}
}
}

0 comments on commit 2626e05

Please sign in to comment.