In Scala 3, is there a way to disable -language:strictEquality (multiversal equality) in a region?

Viewed 93

The particular reason for wanting to do this is to still be able to use pattern matching against a value from a super-class. For instance, I'd like to be able to match with case None when looking at values of type Option[Throwable], but this doesn't seem to be possible since Throwable does not, and never will (I imagine) have a CanEqual instance.

1 Answers

Try limiting the scope of givens like so

  val x: Option[Throwable] = None
  {
    given CanEqual[Option[Throwable], Option[Throwable]] = CanEqual.derived
    x match {
      case Some(v) => v
      case None => new Throwable()
    }
  } // after this brace CanEqual given is out-of-scope

  x match {
    case Some(v) => v
    case None => new Throwable()
  } // compile-time error: Values of types object None and Option[Throwable] cannot be compared with == or !=
Related