Welcome · 1 of 42

Overview

Welcome to the basic Scala tour! It is aimed at anyone who would like to learn Scala’s basics, while writing and running code directly in the browser. On the left is a summary of Scala’s language features that will be covered.

Important: if you don’t understand any of the code on the left, this is OK. And not just OK, it’s expected. It will all be covered in the next steps. Simply click “Next” to get the tour started.

Executable examples

Tip: to execute the code, click the Run button in the editor.

Some fun facts

  • Scala is a statically typed, object-oriented functional language, merging two popular programming approaches into one
  • Created by Martin Odersky at EPFL, launched in 2003, open source; Scala 3 (a major redesign of the language) shipped in 2021
  • Used by Netflix, X (Twitter), LinkedIn, Airbnb, Zalando, Databricks (Apache Spark is written in Scala) and many others
  • It runs on the JVM, and also compiles to JavaScript (Scala.js) and to native binaries (Scala Native)
  • Therefore it has great interop with Java (and any other JVM language)
//type inference, semicolons optional
var number = -1

//Lists are lists...
val list = List(1, 2, 3, 4, 5)

//Maps are maps
val map = Map(1 -> "one", 2 -> "two")

//`return` keyword is optional, so are {} on one liners
def square(x: Int) = x * x

//higher order functions
list.filter(_ > 2).map(_ * 3).sum

//convenient string ops
val strAsNum = "1000".toInt

//easy output (uses System.out.println)
println(strAsNum)

//convenient number ops
number = number.abs

//easy ranges
val range = 1 until 100 by 2

//easy tuples
val tuple = ("Tuples are", 1, true, "awesome thing")

//convenient collection ops
val tsil = list.reverse

//easy iterations
for ((k, v) <- map) println((k, v))

//easy nested loops. Everything is an expression
val result = for (i <- 0 to 10; j <- 0 to i) yield (i, j)

//lambda functions / function literals
val functionLiteral = (n: Int) => math.sqrt(n) * n

//String literals
val longString = """
put " anything  you like in here except three consecutive " :)
"""

//default values for parameters
def lotsOfParams(num: Int = 10, str: String = "N/A") = str * num

//named parameters
lotsOfParams(str = "wat")

//curried methods, partial application
def addNumbers(x: Int)(y: Int) = x + y
val add2 = addNumbers(2)
add2(3)

//easy interfaces / mixins
trait Namable { val name: String; def greet: String = s"Hi $name!" }

//lazy evaluation
trait Randomable { lazy val rand: Int = (math.random() * 100).toInt }

//easy class definition, support for mixins using traits
case class Person(name: String, favoriteLanguage: String) extends Namable with Randomable

val person: Namable & Randomable = Person("Alice", "Scala")

//Pattern Matching
person match {
  case p @ Person(n, fl) => println(s"${p.greet} p.s. we like $fl too!" +
      s" Random number: ${p.rand}. (Still ${p.rand})")
  case _ => println("hm...")
}

//everything is an expression #2
val condition = if person.name == "Alice" then "Hi Alice!" else "Superman?"

//extension methods ("pimp my library")
extension (i: Int)
  def squared = i * i
  def sqrt = math.sqrt(i)

println((7.squared, 49.sqrt))

//Duck typing (structural types)
import scala.reflect.Selectable.reflectiveSelectable
def quackTheDuck(quackable: { def quack: String }) =
  "What does a duck say? " + quackable.quack
class RealDuck { def quack = "Quack!" }
class ImposterDuck { def quack = "Qwaack!" }
quackTheDuck(RealDuck())
quackTheDuck(ImposterDuck())

//Dynamic method calls (don't do it unless you have a good reason...)
import scala.language.dynamics
class Useless extends Dynamic {
  def applyDynamic(name: String)(args: Any*): Unit = {
    println(s"Sorry, I wish I could $name...")
    if (args.nonEmpty) {
      println(s"Here, you can have your ${args.mkString(", ")} back.")
    }
  }
}
val useless = new Useless
useless.reticulate("splines", "marbles")

Contents

Scala basics

  1. Overview
  2. Scalculator
  3. Operators are methods
  4. Variables
  5. Final variables
  6. Printing values
  7. String interpolation
  8. String formatting
  9. Useful operations
  10. Method definition
  11. Method definition 2
  12. Method definition 3
  13. Anonymous functions
  14. Anonymous functions 2
  15. Return multiple values
  16. Declare multiple variables
  17. Assign multiple variables
  18. Loops using while
  19. Loops using for
  20. Loops without loops
  21. If
  22. Match as a switch
  23. Arrays
  24. Lists
  25. Sets
  26. Maps
  27. Mutable collections
  28. Collections: accessing elements
  29. Collections: concatenation
  30. Mutable collection operations
  31. Immutable collections with var
  32. Collections: useful methods
  33. Classes
  34. Classes, continued

What's new in Scala 3

  1. Scala 3: what changed
  2. Optional braces & new control syntax
  3. Top-level definitions & @main
  4. Enums & ADTs
  5. Extension methods
  6. given & using
  7. Union & intersection types
  8. Smaller niceties