Covariant parameter type usage in scalaz.\/

20 views
Skip to first unread message

ace_coder

unread,
Nov 11, 2016, 1:37:28 AM11/11/16
to scalaz
Hi,

I ran into a confusing situation while using covariant parameters with scalaz.\/.

scalaz.\/ is defined with covariant type parameters like so \/[+A, +B], and it works
as expected when I use it in the following manner:

trait Foo[+A, +B] {

  def getFoo(): A \/ B

}

However when I declare it's usage within another monad I get a type error.

trait Foo[ F[ _ ], +A, +B] {

  def getFoo(): F[A \/ B]

}

The above code throws a compile-time error regarding type parameters A and B being 
covariant types at invariant positions, but I thought scalaz.\/ is defined with covariant types.

Am I missing something? Did I forget some other meticulous step in defining the F monad?
Any help/guidance is greatly appreciated. Thank you!

Stephen Compall

unread,
Nov 11, 2016, 1:40:57 PM11/11/16
to sca...@googlegroups.com, ace_coder
On 11/11/16 1:37 AM, ace_coder wrote:
However when I declare it's usage within another monad I get a type error.

trait Foo[ F[ _ ], +A, +B] {
  def getFoo(): F[A \/ B]
}

The above code throws a compile-time error regarding type parameters A and B being 
covariant types at invariant positions, but I thought scalaz.\/ is defined with covariant types.

Yes, I described this problem in "The missing diamond of Scala variance" (video from 17:02, might be a little clearer explanation for your purposes).  Unfortunately, without the "variance variables" described therein, while you can mark F as F[+_], as Scalaz used to, this will restrict you to F-monads that are themselves marked covariant.

--
Stephen Compall

Reply all
Reply to author
Forward
0 new messages