by shigemk2

当面は技術的なことしか書かない

独習Scalazにおける error: covariant type X occurs in invariant position in type [+X]ReaderTOption[C,X] of type l

独習Scalaz 10日目 モナド変換子 にて、エラーになるところを見つけました。

  • scalaz 7.1.x
  • scala 2.11.7

そのままサンプルコードをなぞると、タイトルのようなエラーになる。

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

type StateTReaderTOption[C, S, A] = StateT[({type l[+X] = ReaderTOption[C, X]})#l, S, A]

object StateTReaderTOption extends StateTInstances with StateTFunctions {
  def apply[C, S, A](f: S => (S, A)) = new StateT[({type l[X] = ReaderTOption[C, X]})#l, S, A] {
    def apply(s: S) = f(s).point[({type l[X] = ReaderTOption[C, X]})#l]
  }
  def get[C, S]: StateTReaderTOption[C, S, S] =
    StateTReaderTOption { s => (s, s) }
  def put[C, S](s: S): StateTReaderTOption[C, S, Unit] =
    StateTReaderTOption { _ => (s, ()) }
}

// Exiting paste mode, now interpreting.

<console>:17: error: covariant type X occurs in invariant position in type [+X]ReaderTOption[C,X] of type l
       type StateTReaderTOption[C, S, A] = StateT[({type l[+X] = ReaderTOption[C, X]})#l, S, A]

問題となるのはココ。

type StateTReaderTOption[C, S, A] = StateT[({type l[+X] = ReaderTOption[C, X]})#l, S, A]

共変のパラメータは反変の位置である引数に置けないのでエラーがでます。

とのこと。まあ、そうですよね。。。+XをXにしちゃえば、エラーはなくなります。 というわけで、サイトのソースコードにPRを投げました。無事マージされました。

ソースコード自体はPamfletを使っているようです。

github.com