From 2626e05f9a0f2abf0bcbb23c0089ba84d793e689 Mon Sep 17 00:00:00 2001 From: Andrew Valencik Date: Sun, 27 Mar 2022 11:00:13 -0400 Subject: [PATCH] Add support for Polytypes (#79) * 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 --- pprint/src-2/TPrintImpl.scala | 6 ++++++ pprint/test/src-2/test/pprint/TPrintTests.scala | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/pprint/src-2/TPrintImpl.scala b/pprint/src-2/TPrintImpl.scala index 9a16a3f..aa7ae18 100644 --- a/pprint/src-2/TPrintImpl.scala +++ b/pprint/src-2/TPrintImpl.scala @@ -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) diff --git a/pprint/test/src-2/test/pprint/TPrintTests.scala b/pprint/test/src-2/test/pprint/TPrintTests.scala index 66b9c9b..087399f 100644 --- a/pprint/test/src-2/test/pprint/TPrintTests.scala +++ b/pprint/test/src-2/test/pprint/TPrintTests.scala @@ -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]")) + } } }