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

admit weaker typeclass instances for OptionT when the wrapped Functor… #569

Merged
merged 6 commits into from
Oct 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions core/src/main/scala/cats/data/OptionT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,28 @@ object OptionT extends OptionTInstances {
}
}

// TODO create prioritized hierarchy for Functor, Monad, etc
trait OptionTInstances {
implicit def optionTMonad[F[_]:Monad]: Monad[OptionT[F, ?]] =
new Monad[OptionT[F, ?]] {
trait OptionTInstances1 {
implicit def optionTFunctor[F[_]:Functor]: Functor[OptionT[F, ?]] =
new Functor[OptionT[F, ?]] {
override def map[A, B](fa: OptionT[F, A])(f: A => B): OptionT[F, B] =
fa.map(f)
}
}

trait OptionTInstances extends OptionTInstances1 {
implicit def optionTMonadCombine[F[_]](implicit F: Monad[F]): MonadCombine[OptionT[F, ?]] =
new MonadCombine[OptionT[F, ?]] {
def pure[A](a: A): OptionT[F, A] = OptionT.pure(a)

def flatMap[A, B](fa: OptionT[F, A])(f: A => OptionT[F, B]): OptionT[F, B] =
fa.flatMap(f)

override def map[A, B](fa: OptionT[F, A])(f: A => B): OptionT[F, B] =
fa.map(f)
}

override def empty[A]: OptionT[F,A] = OptionT(F.pure(None))
override def combine[A](x: OptionT[F,A], y: OptionT[F,A]): OptionT[F,A] = x orElse y
}
implicit def optionTEq[F[_], A](implicit FA: Eq[F[Option[A]]]): Eq[OptionT[F, A]] =
FA.on(_.value)
}
13 changes: 9 additions & 4 deletions tests/src/test/scala/cats/tests/OptionTTests.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cats.tests

import cats.{Id, Monad}
import cats.data.{OptionT, Xor}
import cats.laws.discipline.{MonadTests, SerializableTests}
import cats.{Applicative, Id, Monad}
import cats.data.{OptionT, Validated, Xor}
import cats.laws.discipline.{ApplicativeTests, FunctorTests, MonadCombineTests, SerializableTests}
import cats.laws.discipline.arbitrary._
import org.scalacheck.{Arbitrary, Gen}

Expand Down Expand Up @@ -111,6 +111,11 @@ class OptionTTests extends CatsSuite {
}
}

checkAll("OptionT[List, Int]", MonadTests[OptionT[List, ?]].monad[Int, Int, Int])
checkAll("OptionT[List, Int]", MonadCombineTests[OptionT[List, ?]].monad[Int, Int, Int])
checkAll("MonadOptionT[List, ?]]", SerializableTests.serializable(Monad[OptionT[List, ?]]))

{
implicit val F = ListWrapper.functor
checkAll("Functor[OptionT[ListWrapper, ?]]", FunctorTests[OptionT[ListWrapper, ?]].functor[Int, Int, Int])
}
}