Scala's static type system

Introduction to Scala

David Venturi

Curriculum Manager, DataCamp

More answers to "Why use Scala?"

Scala combines object-oriented and functional programming in one concise, high-level language. Scala's static types help avoid bugs in complex applications, and its JVM and JavaScript runtimes let you build high-performance systems with easy access to huge ecosystems of libraries.

Introduction to Scala

More answers to "Why use Scala?"

Scala combines object-oriented and functional programming in one concise, high-level language. Scala's static types help avoid bugs in complex applications, and its JVM and JavaScript runtimes let you build high-performance systems with easy access to huge ecosystems of libraries.

Introduction to Scala

Some definitions

  • Type: restricts the possible values to which a variable can refer, or an expression can produce, at run time
Introduction to Scala

Scala value types have equivalent Java types

Scala types
  • scala.Double
  • scala.Float
  • scala.Long
  • scala.Int
  • scala.Short
  • scala.Byte
  • scala.Char
  • scala.Boolean
  • scala.Unit

Java types

  • java.lang.Double
  • java.lang.Float
  • java.lang.Long
  • java.lang.Integer
  • java.lang.Short
  • java.lang.Byte
  • java.lang.Character
  • java.lang.Boolean
Introduction to Scala

Some definitions

  • Type: restricts the possible values to which a variable can refer, or an expression can produce, at run time

  • Compile time: when source code is translated into machine code, i.e., code that a computer can read

  • Run time: when the program is executing commands (after compilation, if compiled)

Introduction to Scala

Type systems

Static type systems

A language is statically typed if the type of a variable is known at compile time. That is, types checked before run-time.

  • C/C++
  • Fortran
  • Java
  • Scala

Dynamic type systems

A language is dynamically typed if types are checked on the fly. That is, types are checked during execution (i.e., run time).

  • JavaScript
  • Python
  • Ruby
  • R
Introduction to Scala

Pros of static type systems

  • Increased performance at run time
  • Properties of your program verified (i.e., prove the absence of common type-related bugs)
  • Safe refactorings
  • Documentation in the form of type annotations (: Int in val fourHearts: Int = 4)

Cons of static type systems

  • It takes time to check types (i.e., delay before execution)
  • Code is verbose (i.e., code is longer/more annoying to write)
  • The language is not flexible (e.g., one strict way of composing a type)
Introduction to Scala

Reducing verbosity (with variables)

Without type inference

scala> val fourHearts: Int = 4
fourHearts: Int = 4

With type inference

scala> val fourHearts = 4
fourHearts: Int = 4
Introduction to Scala

Reducing verbosity (with collections)

Without type inference

scala> val players: Array[String] = Array("Alex", "Chen", "Marta")
players: Array[String] = Array(Alex, Chen, Marta)

With type inference

scala> val players = Array("Alex", "Chen", "Marta")
players: Array[String] = Array(Alex, Chen, Marta)
Introduction to Scala

Promoting flexibility

  • Pattern matching
  • Innovative ways to write and compose types
Introduction to Scala

Compiled, statically-typed languages

Compiled languages
  • Increased performance at run time
Statically-typed languages
  • Increased performance at run time
Introduction to Scala

Let's practice!

Introduction to Scala

Preparing Video For Download...