scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
232 stars 21 forks source link

Option.fold and Either.fold have inconsistent parameter lists (and Option.fold has worse type inference) #10642

Open SethTisue opened 6 years ago

SethTisue commented 6 years ago

Either#fold has one parameter list,def fold[C](fa: A => C, fb: B => C): C

but Option#fold has two, def fold[B](ifEmpty: => B)(f: A => B): B

as shown in #9262, the single-parameter-list version offers better type inference

it's also simply confusing that the two are different

fwiw, the difference goes away in Dotty, which can infer the right type either way:

scala> Option(5).fold(Left("err"))(Right(_)) 
val res0: scala.util.Either[String, Int] = Right(5)

given that the improvement in type inference is coming in Dotty, it might still be worth bringing the two in line for consistency's sake? I fear it's not worth breaking existing code

reported by @Jacoby6000 on #scala IRC

SethTisue commented 6 years ago

Option.fold was added by @paulp in 2012. it's unclear to me if the question of 1-or-2-parameter-lists was considered at the time or not

there is also the question of consistency with the signature of Iterable.fold and friends, of course; that might have been the motivation for going with two parameter lists