Scala 3 for Scala Haters

Karl Bielefeldt

The syntax is too busy


object Main {
  def main(args: Array[String]): Unit = {
    val xs = List(-2, -1, 0, 1, 2)
    val ys = List(3, 4, 5, 6, 7)

    val result = for {
      x <- xs
      y <- ys
    } yield if (x > 0) x else y

    result.foreach(println)
  }
}

  @main def main =
    val xs = List(-2, -1, 0, 1, 2)
    val ys = List(3, 4, 5, 6, 7)

    val result = for
      x <- xs
      y <- ys
    yield if x > 0 then x else y

    result.foreach(println)

                  TypeIActuallyWant with Product with Serializable

                  TypeIActuallyWant

Nulls can't be checked at compile time


-- [E007] Type Mismatch Error: Main.scala:2:23
2 |  val string: String = null
  |                       ^^^^
  |                       Found:    Null
  |                       Required: String

It doesn't make sense to check for equality between different types


-- Error: Main.scala:2:2
2 |  "string" == 42
  |  ^^^^^^^^^^^^^^
  |  Values of types String and Int cannot be compared
  |  with == or !=

Infix operators are inconsistent


-- Error: Main.scala:6:7
6 |  list combine other
  |       ^^^^^^^
  |Alphanumeric method combine is not declared `infix`;
  |it should not be used as infix operator.  The operation
  |can be rewritten automatically to `combine` under
  | -deprecation -rewrite. Or rewrite to method syntax
  |.combine(...) manually.

Why do I have to use 'new' sometimes and not other times?


class MyClass(arg: Int)

// In Scala 2, this gives error: not found: value MyClass
// Works fine in Scala 3
MyClass(1)

Extension methods are confusing


extension (c: Circle)
  def circumference: Double = c.radius * math.Pi * 2

This is going to be another Python 2 -> 3 incompatibility fiasco

  • Always been compatible with Java
  • Can use 3.0 libraries from 2.13 code
  • Can use 2.13 libraries (except macros) from 3.0 code
  • 3.0 published libraries are discouraged from depending on 2.13 libraries
  • The 2.13 standard library is the official 3.0 standard library
  • Many new features are opt-in (-Yexplicit-nulls, -Ysafe-init, -language:strictEquality)
  • Many removed features are only deprecated until 3.1+ (-source:future)
  • -explain and -explain-types
  • 2.13 compiler supports -Xsource:3 to start migration
  • 3.0 compiler has Migration mode with rewrite
  • Migration Guide

Many nice things for the Scala lovers

  • Intersection types
  • Union types
  • Type lambdas
  • Match types
  • Better enums
  • Easier type classes
  • Better conversions
  • Better macros
  • Performance improvements such as opaque types, inline, and @threadUnsafe

Questions?

https://dotty.epfl.ch/docs/