Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Higher kinded type classes crash compiler with opaque types #5527

Closed
adamgfraser opened this issue Nov 27, 2018 · 0 comments
Closed

Higher kinded type classes crash compiler with opaque types #5527

adamgfraser opened this issue Nov 27, 2018 · 0 comments

Comments

@adamgfraser
Copy link
Contributor

adamgfraser commented Nov 27, 2018

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"))
}
abgruszecki added a commit that referenced this issue Dec 17, 2018
Fix #5527: Fix companion module for hk opaque type aliases
nicolasstucki pushed a commit to dotty-staging/dotty that referenced this issue Jan 5, 2019
Higher kinded opaque aliases have a type lambda as right hand side. This
needs to be taken account of when computing the companion module of these
aliases.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant