Skip to content

Instantly share code, notes, and snippets.

@ceedubs
Created April 25, 2015 16:49
Show Gist options
  • Save ceedubs/f29df9d30ddd4b4994da to your computer and use it in GitHub Desktop.
Save ceedubs/f29df9d30ddd4b4994da to your computer and use it in GitHub Desktop.
Type inference of curried vs uncurried fold on Option
object CurryInference {
implicit final class OptionOps[A](val oa: Option[A]) {
/** curried fold */
def curriedFold[B](z: => B)(f: A => B): B = oa.fold(z)(f)
/** uncurried fold */
def uncurriedFold[B](z: => B, f: A => B): B = oa.fold(z)(f)
}
val age: Option[Int] = Some(19)
/*
* error: type mismatch;
* found : scala.util.Right[Nothing,Int]
* required: scala.util.Left[String,Nothing]
* age.curriedFold(Left("age is required"))(i => if (i >= 0) Right(i) else Left(s"$i is not positive"))
* ^
*/
//age.curriedFold(Left("age is required"))(i => if (i >= 0) Right(i) else Left(s"$i is not positive"))
/*
* Compiles. Without currying, LUB is determined to be Either[String, Int]
*
* Technically the LUB probably also has Product with Serializable
*/
age.uncurriedFold(Left("age is required"), i => if (i >= 0) Right(i) else Left(s"$i is not positive"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment