Skip to content

Instantly share code, notes, and snippets.

@hamnis
Last active January 10, 2017 10:51
Show Gist options
  • Save hamnis/af6431acc611126c6dc814fce3323dcf to your computer and use it in GitHub Desktop.
Save hamnis/af6431acc611126c6dc814fce3323dcf to your computer and use it in GitHub Desktop.
Odd scala 2.12 problem

This code works in scala 2.11

import scalaz._

type Id[+A] = A

implicit val monadInstance: Monad[Id] = new Monad[Id] {
  override def point[A](a: => A): Id[A] = a

  override def bind[A, B](fa: Id[A])(f: (A) => Id[B]): Id[B] = f(fa)
}

But this fails in Scala 2.12.1 with this error message:

<console>:15: error: covariant type Id occurs in invariant position in type => scalaz.Monad[Id] of value monadInstance
       implicit val monadInstance: Monad[Id] = new Monad[Id] {

But If i move the Definition of type Id to a different compilation unit.

 scala> type Id[+A] = A
defined type alias Id

 scala> :paste
// Entering paste mode (ctrl-D to finish)

implicit val monadInstance: scalaz.Monad[Id] = new scalaz.Monad[Id] {
  override def point[A](a: => A): Id[A] = a

  override def bind[A, B](fa: Id[A])(f: (A) => Id[B]): Id[B] = f(fa)
}

// Exiting paste mode, now interpreting.

monadInstance: scalaz.Monad[Id] = $anon$1@3a03f5d4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment