1 of 51

Making the Transition�to Scala 3

Seth Tisue / Lightbend, Inc.

Twitter: @SethTisue�GitHub: SethTisue

2 of 51

Scala in a nutshell

Scala = object-oriented programming + functional programming

targets: JVM (Java Virtual Machine),� but also JavaScript (Scala.js) and LLVM (Scala Native)

built around a Java-like core;� Java programmers can begin by writing Java-like code

Scala prioritizes type safety

normal Scala code uses immutable values by default

3 of 51

Scala 2 timeline

Scala 3 timeline

2012 — first commit

�2020 — Dotty (experimental compiler)� becomes Scala 3

2021 — 3.0 (May), 3.1 (just released!)

2003 — first commit

2007 — Scala 2.6

2008 — Scala 2.7

2010 — Scala 2.8

2012 — Scala 2.9, 2.10

2014 — Scala 2.11

2016 — Scala 2.12

2019 — Scala 2.13

2021 — Scala 2.13.6

Scala @ Twitter

Spark

Akka

Kafka

Reactive Manifesto

4 of 51

The Scala organization

nonprofit / academic

companies

Prof. Martin Odersky

5 of 51

Who am I?

Full-time Scala coder (2008–)

Scala community organizer (2009–2016)

Scalawags podcast (2014–2016)

Scala Improvement Process committee (2014–)

Compiler hacking, open source, community relations

@ Lightbend (2015–)

6 of 51

  1. Why upgrade?�
  2. When to upgrade?�
  3. How to upgrade?

7 of 51

  • Why upgrade?�
  • When to upgrade?�
  • How to upgrade?

8 of 51

Why upgrade?

because Scala 3 is…

  • the samehuge emphasis on compatibility�& smooth upgrade paths
  • yet better!tons of features & benefits

9 of 51

Scala 3 is still Scala

The Scala 3 uses the Scala 2.13 standard library AS IS.

10 of 51

Scala 3 is still Scala

Nearly all Scala 2 code is also�valid Scala 3 code.

11 of 51

Scala 3 is still Scala

Library & tooling ecosystem is already large.

12 of 51

Scala 3: benefits

13 of 51

Quality

stronger theoretical foundations� Scala 3 = Dotty = DOT = Dependent Object Types

stronger practical foundations� new compiler = less tech debt

14 of 51

“Fixed in Scala 3”

(the true numbers are�surely much higher)

15 of 51

Benefits: UX

Design goal:

  • Deprecate or remove unneeded features

16 of 51

Benefits: UX

Design goal:

  • Bake common usage patterns into the language

Features are “new”, yet already familiar

17 of 51

Benefits: Implicits

“Implicits” redesigned

  • extension methods are now supported directly
  • given (at definition site) and using (at use site) replace implicit
  • givens rarely need names anymore

18 of 51

Benefits: Implicits

“Implicits” redesigned

  • implicit conversions now safer,�yet discouraged
  • “context functions” enable new coding patterns

19 of 51

Benefits: Context functions

Brachthäuser,�Boruch-Gruszecki,�& Odersky��ICFP 2021��https://www.youtube.com/watch?v=UmO-f0qTRSU

20 of 51

Benefits: Syntax

cleaner, indentation-based syntax

Scala 2

Scala 3

21 of 51

Benefits: Enumerations

Enumerations

  • Simple enumerations, like Java
  • More complex use cases

22 of 51

Benefits: Enumerations

Enumerations — simple

Scala 2

Scala 3

23 of 51

Benefits: Enumerations

Enumerations — advanced

Scala 2

Scala 3

24 of 51

Benefits: Opaque types

“Opaque types” provide zero-overhead abstraction

25 of 51

Benefits: Metaprogramming

new metaprogramming features

Scala 2: experimental macro system� workable, but cumbersome and fragile to create

Scala 3:

  • macros needed less often
  • new, superior macro system fully supported

26 of 51

Benefits: Metaprogramming

27 of 51

Benefits: Metaprogramming

Macros are needed less often.

  • inline enables zero-cost abstraction
  • “Match types” replace macro-based or implicit-based type-level computations
  • “Typeclass derivation” is supported directly

28 of 51

Benefits

also, talks yesterday by:�• Dean Wampler�• Bill Venners

29 of 51

  • Why upgrade?�
  • When to upgrade?�
  • How to upgrade?

30 of 51

Advice

Be bold!

Type safety will protect you.

Strong compatibility story will protect you.

Yet be cautious, too...

31 of 51

Advice

  • Start small!
  • Start with small projects
  • Start with a greenfield project
  • Start with internal projects

32 of 51

Advice

Migrate advanced, Scala-friendly teams first

Scala 2 remains the safest choice�• for less experienced teams�• for less enthusiastic teams

33 of 51

Advice on dependencies

  • Scala 3 can use 2’s libraries

Caveat #1: Macros

Caveat #2: Diamond dependencies

34 of 51

Advice on dependencies

  • Be cautious when using Scala 2 libraries from Scala 3�use CrossVersion.for3Use2_13�in applications, not libraries

35 of 51

App 2

Library C for 3

Library D for 3

Library E for 3

Library E for 2.13

for3Use2_13

App 1

Library A for 3

for3Use2_13

Library B for 2.13

Library D�shouldn’t�do this!

36 of 51

Advice

  • Consider all of your tooling

IDE support is already pretty good�(IntelliJ, Metals + VS Code)

37 of 51

Advice

  • Consider your tooling

3.0 shipped when:

design decisions were made�quality & tooling good enough to create Scala 3 libraries with

3.0 did not wait for:� tooling to be fully on par with Scala 2

38 of 51

  • Why upgrade?�
  • When to upgrade?�
  • How to upgrade?

39 of 51

Advice

  • Get on Scala 2.13 first
  • Drop Scala 2.12 if you can
  • Clean up warnings
  • Enable -Xsource:3

40 of 51

Advice on cross-building

  • Enable -Xsource:3 today
  • Start cross-building very soon
  • Leave 2 behind when ready

41 of 51

Advice

42 of 51

Advice

43 of 51

Advice

  • Use -source:3.0-migration
  • Use -rewrite

44 of 51

Advice

  • Choose a strategy for adopting indentation-based syntax

Not at all?�Gradually?� by file, by class, by method�All at once?

most common?

45 of 51

Learning materials: Videos

“Let’s Talk About Scala 3”�12 videos, 10–25 mins each

46 of 51

Learning materials: Free online courses

new course:

the classics, updated:

47 of 51

Learning materials: The Scala 3 Book

free on Scala website

author: Alvin Alexander

https://docs.scala-lang.org/scala3/book/introduction.html

48 of 51

Learning materials: Books

49 of 51

Learning materials: Books

MEAP = in progress

“Functional Programming in Scala”

by Runar Bjarnason & Paul Chiusano��new co-author:�Michael Pilquist (of Typelevel, FS2 fame)

50 of 51

Questions?

51 of 51

FAQ

  1. How is Scala 3 adoption going?
  2. Do you like the new indentation-based syntax?
  3. What IDE do you recommend?
  4. When will {Akka, Play, Spark, sbt, …} move to Scala 3?