twitter / bijection

Reversible conversions between types
Apache License 2.0
657 stars 125 forks source link

Add InjectionK and BijectionK #271

Open MansurAshraf opened 7 years ago

MansurAshraf commented 7 years ago

Cats have InjectionK but their invert function returns Option[F[_]] which they are unwilling to change. It would nice to have InjectionK with invert defined as Try[F[_]] as we have for all our existing injections. While Option[F[_]] maybe more theoretically correct, I have found Try[F[_]] to be more useful as I usually want to do something with he exception when inversion fails!. We would also need FunctionK as well

johnynek commented 7 years ago

I think this is a great idea.

Of course, I will likely not get to it very soon. Something like:

trait BijectionK[F[_], G[_]] {
  def apply[A](f: F[A]): G[A]
  def invert[A](g: G[A]): F[A]

  def toBijection[A]: Bijection[F[A], G[A]]
}

trait InjectionK[F[_], G[_]] {
  def apply[A](f: F[A]): G[A]
  def invert[A](g: G[A]): Try[F[A]]

  def toInjection[A]: Injection[F[A], G[A]]
}

and of course laws.

Maybe add FunctionK while we are at it, but maybe not actually needed... not clear.