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