scala/scala3

Regression: PartialFunction.applyOrElse executed despite being defined

Closed this issue · 1 comments

Compiler version

3.3.3

Minimized code

import cats.Functor
import cats.implicits.given
import scala.util.boundary

extension [CC[_]: Functor, A](coll: CC[A])
  /** Applies the partial function to every argument to narrow the type, but instead of dropping unmatched elements like
    * [[Seq.collect]], returns `None` for the entire list.
    */
    def collectAllOrNone[B](pf: PartialFunction[A, B]): Option[CC[B]] =
      boundary {
        Some(coll.map { x =>
          println(pf.isDefinedAt(x))
          pf.applyOrElse(x, { println("break"); boundary.break(None) })
        })
      }

Output

The fallback boundary.break(None) is executed, despite pf.isDefined(x) == true.

Expectation

Fallback should only be evaluated when the partial function is not defined.

Nevermind, I was stupid. default: A => B is actually a function, not by name parameter and since break: Nothing <: A => B, it was evaluated as a block, not a function.