Skip to content

Commit

Permalink
make B in Const[A,B] covariant to help inference
Browse files Browse the repository at this point in the history
  • Loading branch information
kailuowang committed Apr 12, 2017
1 parent 100e261 commit 91d3933
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
17 changes: 6 additions & 11 deletions core/src/main/scala/cats/data/Const.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import cats.functor.Contravariant
/**
* [[Const]] is a phantom type, it does not contain a value of its second type parameter `B`
* [[Const]] can be seen as a type level version of `Function.const[A, B]: A => B => A`
* B is set covariant to help type inference.
*/
final case class Const[A, B](getConst: A) {
final case class Const[A, +B](getConst: A) {
/**
* changes the type of the second type parameter
*/
def retag[C]: Const[A, C] =
this.asInstanceOf[Const[A, C]]

def combine(that: Const[A, B])(implicit A: Semigroup[A]): Const[A, B] =
def combine[BB >: B](that: Const[A, BB])(implicit A: Semigroup[A]): Const[A, BB] =
Const(A.combine(getConst, that.getConst))

def traverseFilter[F[_], C](f: B => F[Option[C]])(implicit F: Applicative[F]): F[Const[A, C]] =
Expand All @@ -23,13 +24,13 @@ final case class Const[A, B](getConst: A) {
def traverse[F[_], C](f: B => F[C])(implicit F: Applicative[F]): F[Const[A, C]] =
F.pure(retag[C])

def ===(that: Const[A, B])(implicit A: Eq[A]): Boolean =
def ===[BB >: B](that: Const[A, BB])(implicit A: Eq[A]): Boolean =
A.eqv(getConst, that.getConst)

def partialCompare(that: Const[A, B])(implicit A: PartialOrder[A]): Double =
def partialCompare[BB >: B](that: Const[A, BB])(implicit A: PartialOrder[A]): Double =
A.partialCompare(getConst, that.getConst)

def compare(that: Const[A, B])(implicit A: Order[A]): Int =
def compare[BB >: B](that: Const[A, BB])(implicit A: Order[A]): Int =
A.compare(getConst, that.getConst)

def show(implicit A: Show[A]): String =
Expand All @@ -39,12 +40,6 @@ final case class Const[A, B](getConst: A) {
object Const extends ConstInstances {
def empty[A, B](implicit A: Monoid[A]): Const[A, B] =
Const(A.empty)

class OfPartiallyApplied[B] {
def apply[A](a: A): Const[A, B] = Const(a)
}

def of[B]: OfPartiallyApplied[B] = new OfPartiallyApplied
}

private[data] sealed abstract class ConstInstances extends ConstInstances0 {
Expand Down
2 changes: 1 addition & 1 deletion tests/src/test/scala/cats/tests/RegressionTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class RegressionTests extends CatsSuite {

test("#500: foldMap - traverse consistency") {
assert(
List(1,2,3).traverse(i => Const.of[List[Int]](List(i))).getConst == List(1,2,3).foldMap(List(_))
List(1,2,3).traverse(i => Const(List(i))).getConst == List(1,2,3).foldMap(List(_))
)
}

Expand Down

0 comments on commit 91d3933

Please sign in to comment.