@@ -3455,5 +3455,35 @@ end IllegalUnrollPlacement
34553455
34563456class BadFormatInterpolation (errorText : String )(using Context ) extends Message (FormatInterpolationErrorID ):
34573457 def kind = MessageKind .Interpolation
3458- def msg (using Context ) = errorText
3459- def explain (using Context ) = " "
3458+ protected def msg (using Context ) = errorText
3459+ protected def explain (using Context ) = " "
3460+
3461+ class MatchIsNotPartialFunction (using Context ) extends SyntaxMsg (MatchIsNotPartialFunctionID ):
3462+ protected def msg (using Context ) =
3463+ " match expression in result of block will not be used to synthesize partial function"
3464+ protected def explain (using Context ) =
3465+ i """ A `PartialFunction` can be synthesized from a function literal if its body is just a pattern match.
3466+ |
3467+ |For example, `collect` takes a `PartialFunction`.
3468+ | (1 to 10).collect(i => i match { case n if n % 2 == 0 => n })
3469+ |is equivalent to using a "pattern-matching anonymous function" directly:
3470+ | (1 to 10).collect { case n if n % 2 == 0 => n }
3471+ |Compare an operation that requires a `Function1` instead:
3472+ | (1 to 10).map { case n if n % 2 == 0 => n case n => n + 1 }
3473+ |
3474+ |As a convenience, the "selector expression" of the match can be an arbitrary expression:
3475+ | List("1", "two", "3").collect(x => Try(x.toInt) match { case Success(i) => i })
3476+ |In this example, `isDefinedAt` evaluates the selector expression and any guard expressions
3477+ |in the pattern match in order to report whether an input is in the domain of the function.
3478+ |
3479+ |However, blocks of statements are not supported by this idiom:
3480+ | List("1", "two", "3").collect: x =>
3481+ | val maybe = Try(x.toInt) // statements preceding the match
3482+ | maybe match
3483+ | case Success(i) if i % 2 == 0 => i // throws MatchError on cases not covered
3484+ |
3485+ |This restriction is enforced to simplify the evaluation semantics of the partial function.
3486+ |Otherwise, it might not be clear what is computed by `isDefinedAt`.
3487+ |
3488+ |Efficient operations will use `applyOrElse` to avoid computing the match twice,
3489+ |but the `apply` body would be executed "per element" in the example. """
0 commit comments