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 documentation of PartialFunction::applyOrElse says:
For all partial function literals the compiler generates an applyOrElse implementation which avoids double evaluation of pattern matchers and guards.
However this is not currently the case in Dotty:
objectTest {
private[this] varcount=0deftest(x: Int) = { count +=1; true }
objectFoo {
defunapply(x: Int):Option[Int] = { count +=1; Some(x) }
}
defmain(args: Array[String]):Unit= {
valres=List(1, 2).collect { case x if test(x) => x }
println(count) // prints 4 but should be 2
count =0valres2=List(1, 2).collect { caseFoo(x) => x }
println(count) // prints 4 but should be 2
}
}