Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

scala

scala-3

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.

like image 651
bbarker Avatar asked Aug 11 '21 18:08

bbarker


People also ask

What changed in Scala 3?

Besides greatly improved type inference, the Scala 3 type system also offers many new features, giving you powerful tools to statically express invariants in the types: Enumerations. Enums have been redesigned to blend well with case classes and form the new standard to express algebraic data types. Opaque Types.

Does spark work with Scala 3?

The answer is: it doesn't matter! We can already use Scala 3 to build Spark applications thanks to the compatibility between Scala 2.13 and Scala 3.

Is Scala written in Java?

Scala runs on the Java platform (Java virtual machine) and is compatible with existing Java programs.

Is Scala similar to JavaScript?

At a high level, Scala shares these similarities with JavaScript: Both are considered high-level programming languages, where you don't have to concern yourself with low-level concepts like pointers and manual memory management. Both have a relatively simple, concise syntax.


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 !=
like image 169
Mario Galic Avatar answered Sep 22 '22 20:09

Mario Galic