diff options
author | Georgi Krastev <joro.kr.21@gmail.com> | 2017-11-29 19:53:01 +0100 |
---|---|---|
committer | Georgi Krastev <joro.kr.21@gmail.com> | 2017-11-29 20:05:50 +0100 |
commit | d8fb2f5ca2edc1a34ed7831f06d5eca08ba4989c (patch) | |
tree | 879238ab17d24e05457d158e6af76879a53aea5a /tests/src/main | |
parent | 93ff9742cf8cd9f2c60986ff4f7af8d24e268b1f (diff) | |
download | magnolia-d8fb2f5ca2edc1a34ed7831f06d5eca08ba4989c.tar.gz magnolia-d8fb2f5ca2edc1a34ed7831f06d5eca08ba4989c.tar.bz2 magnolia-d8fb2f5ca2edc1a34ed7831f06d5eca08ba4989c.zip |
Existentially abstract unbound subtype parameters
That happens when the subtype of a sealed trait has more type
parameters than its parent. When those extra type parameters are
covariant they are replaced by their upper bounds, otherwise they
are existentially quantified.
Diffstat (limited to 'tests/src/main')
-rw-r--r-- | tests/src/main/scala/tests.scala | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/tests/src/main/scala/tests.scala b/tests/src/main/scala/tests.scala index 4b0e5c1..bc33969 100644 --- a/tests/src/main/scala/tests.scala +++ b/tests/src/main/scala/tests.scala @@ -47,6 +47,14 @@ object Test { def apply(a: String, b: String): Test = Test(Param(a, b)) } +sealed trait Politician[+S] +case class Accountable[+S](slogan: S) extends Politician[S] +case class Corrupt[+S, +L <: Seq[Company]](slogan: S, lobby: L) extends Politician[S] + +sealed trait Box[+A] +case class SimpleBox[+A](value: A) extends Box[A] +case class LabelledBox[+A, L <: String](value: A, var label: L) extends Box[A] + object Tests extends TestApp { def tests() = for (i <- 1 to 1000) { @@ -201,5 +209,20 @@ object Tests extends TestApp { test("serialize a value class") { Show.gen[Length].show(new Length(100)) }.assert(_ == "100") + + // Corrupt being covariant in L <: Seq[Company] enables the derivation for Corrupt[String, _] + test("show a Politician with covariant lobby") { + Show.gen[Politician[String]].show(Corrupt("wall", Seq(Company("Alice Inc")))) + }.assert(_ == "Corrupt(slogan=wall,lobby=[Company(name=Alice Inc)])") + + // LabelledBox being invariant in L <: String prohibits the derivation for LabelledBox[Int, _] + test("can't show a Box with invariant label") { + scalac"Show.gen[Box[Int]]" + }.assert { _ == TypecheckError( + txt"""magnolia: could not find typeclass for type L + | in parameter 'label' of product type magnolia.tests.LabelledBox[Int, _ <: String] + | in coproduct type magnolia.tests.Box[Int] + |""") + } } } |