Closed
Description
Defining an instance for a higher kinded type class (e.g. Contravariant
as opposed to Semigroup
) in the type companion to an opaque type and trying to summon it implicitly causes the compiler to crash. Note that if the instance is imported explicitly the program will run correctly. But I don't think this should be necessary as implicit instances in the type companion should be available without explicit import and at the very least an an implicit not found error should be generated rather than the compiler crashing. See example below.
trait Contravariant[F[_]] {
def contramap[A, B](fa: F[A])(f: B => A): F[B]
}
object Library {
opaque type Set[A] = A => Boolean
object Set {
def singleton[A](a: A): Set[A] =
_ == a
implicit class SetOps[A](private val set: Set[A]) extends AnyVal {
def contains(a: A): Boolean =
set(a)
}
implicit val setContravariant: Contravariant[Set] = new Contravariant[Set] {
def contramap[A, B](fa: Set[A])(f: B => A): Set[B] =
b => fa(f(b))
}
}
}
object User extends App {
import Library._
//import Library.Set.setContravariant if this is imported the program will run correctly
val F = implicitly[Contravariant[Set]]
val one = Set.singleton(1)
val char = F.contramap(one)((s: String) => s.length)
assert(char.contains("a"))
assert(!char.contains("ab"))
}
Metadata
Metadata
Assignees
Labels
No labels