E186: Implausible Pattern Warning

This warning is emitted when a pattern match case appears implausible—the pattern could only match the selector type if there is a custom equals method that identifies elements of the two unrelated types.

This typically occurs when matching against singleton objects or values of unrelated types, where the pattern is technically possible due to Java's equals semantics but is unlikely to be intentional.

This warning requires the -Wimplausible-patterns compiler flag.


Example

//> using options -Wimplausible-patterns

abstract class Recovery
object Recovery extends Recovery

abstract class TypedRecovery
object TypedRecovery extends TypedRecovery

def example(x: TypedRecovery): Unit = x match
  case Recovery => println("Recovery")
  case TypedRecovery => println("TypedRecovery")

Error

-- [E186] Type Warning: example.scala:10:7 -------------------------------------
10 |  case Recovery => println("Recovery")
   |       ^^^^^^^^
   |Implausible pattern:
   |Recovery  could match selector of type  TypedRecovery
   |only if there is an `equals` method identifying elements of the two types.

Solution

Remove the implausible pattern if it was unintentional:

//> using options -Wimplausible-patterns

abstract class Recovery
object Recovery extends Recovery

abstract class TypedRecovery
object TypedRecovery extends TypedRecovery

def example(x: TypedRecovery): Unit = x match
  case TypedRecovery => println("TypedRecovery")

Or use a common supertype if you need to match against both:

//> using options -Wimplausible-patterns

trait RecoveryEvent

abstract class Recovery extends RecoveryEvent
object Recovery extends Recovery

abstract class TypedRecovery extends RecoveryEvent
object TypedRecovery extends TypedRecovery

def example(x: RecoveryEvent): Unit = x match
  case Recovery => println("Recovery")
  case TypedRecovery => println("TypedRecovery")