You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The behavior of Either.filterOrElse is confusing when the predicate doesn't match; it converts Either[A, B] to Either[AA, B] where AA is the "zero" element provided to filterOrElse. This is unexpected because for everything else, a filter operation results in a possible reduction of the number of elements in the container, and doesn't actually change the value of the elements. What I'd expect is to get an Option[Right] = None if the Either is a right and the predicate doesn't match and a Option[Right] = Some(a) otherwise. If it's a Left, the existing behavior of no-op is expected.
This causes subtle bugs in the code like the following:
Call: brokers(() => Nil, (xs: List[Int]) => throw new RuntimeException("throw2"))
Exception in thread "main" java.lang.ClassCastException: scala.collection.immutable.Nil$ cannot be cast to java.lang.Throwable at Practice$.brokers(Practice.scala:57) at Practice$.delayedEndpoint$Practice$1(Practice.scala:63)
First of all, the Throwable is lost causing a compile error (shown by the cast).
It converted the Either[Throwable,List[Int]] = Right(List()) to an Either[java.io.Serializable,List[Int]] = Left(List()). Thus the compile error, and the match with case Left and eventual exception. The Serializable must have come from the contravariant type parameter AA >: A, because the first common supertype for Throwable and List is a Serializable.
The text was updated successfully, but these errors were encountered:
@SethTisue said:
I'd suggest starting a mailing list thread on this, and if you can gather some consensus on whether some change would be desirable, then come back to JIRA. filterOrElse is working as designed.
I'm closing the ticket, but I don't mean to dismiss your concern. See how the mailing list thread goes. (I already have some things in mind I will say on that thread if you start it.)
The behavior of
Either.filterOrElse
is confusing when the predicate doesn't match; it convertsEither[A, B]
toEither[AA, B]
whereAA
is the "zero" element provided tofilterOrElse
. This is unexpected because for everything else, a filter operation results in a possible reduction of the number of elements in the container, and doesn't actually change the value of the elements. What I'd expect is to get anOption[Right] = None
if theEither
is a right and the predicate doesn't match and aOption[Right] = Some(a)
otherwise. If it's aLeft
, the existing behavior of no-op is expected.This causes subtle bugs in the code like the following:
Call:
brokers(() => Nil, (xs: List[Int]) => throw new RuntimeException("throw2"))
First of all, the
Throwable
is lost causing a compile error (shown by the cast).It converted the
Either[Throwable,List[Int]] = Right(List())
to anEither[java.io.Serializable,List[Int]] = Left(List())
. Thus the compile error, and the match with caseLeft
and eventual exception. TheSerializable
must have come from the contravariant type parameterAA >: A
, because the first common supertype forThrowable
andList
is aSerializable
.The text was updated successfully, but these errors were encountered: