Mike Slinn

Scala Overview and Philosophy

— Draft —

Published 2019-08-12. Last modified 2024-07-20.
Time to read: 9 minutes.

Scala runs on the Java virtual machine (JVM) and JavaScript. It has a powerful type system and strong support for both object-oriented and functional programming.

What Is Scala?

Scala is a Java-compatible language that is both object-oriented and functional, and was designed to support large-scale computing. It is succinct and is designed to be the basis of domain-specific languages (DSLs). Scala is strongly typed, yet can be used in an interpretive fashion in many cir­cum­stances.

Scala has a minimum of control structures, because its design encourages data-driven programming. Python’s data structures resemble Scala’s in this fashion. Unlike Python, but like Ruby, Scala allows you to design your own control structures.

Scala is Compatible with Java

Because the primary platform for Scala is the Java virtual machine (JVM), Scala code running on the JVM can interoperate with Java code. That means all of the Java code ever written can be directly invoked from Scala, and Scala functionality can be accessed from Java.

If your company has a big investment in Java, then you can enhance that investment by augmenting it with Scala code. Scala can also interoperate with other JVM languages.

The JVM Specification describes the JVM.

Everything is an Object

Numbers and strings are objects. Objects are normally defined with methods, and Scala also provides a mechanism called implicits that allows you to associate new methods with objects, without changing the object definitions. This means that your domain model need not get tangled up with your business logic.

A Scala String is not the same as a Java String, because a Scala String is an object but a Java String is not. However, there is an implicit conversion (defined in Predef.scala) that automatically allows Java Strings to be viewed as Scala Strings. This means that Java Strings can be used in a Scala program, and they can be treated just as if they were Scala Strings. The converse is also true: Scala Strings can be passed to Java code, and they will be converted to Java Strings automagically. We will discuss implicit conversions in the Implicit Conversions lecture of the Intermediate Scala course. Similarly, Java ints and Integers can be used in a Scala program as if they were Scala Ints, and Java longs and Longs can be used as Scala Longs because Predef.scala automatically includes implicit conversions.

Functions are objects too, and can be manipulated and passed around in interesting ways. Scala supports concepts like higher-order functions, partially bound functions and functions that are only defined for ranges of input. We will cover these topics in detail in the Higher-Order Functions, Partially Applied Functions, and Partial Functions lectures of the Intermediate Scala course.

Scala avoids the “is a class an object?” question by not having class variables or methods (Java and C++ call these static variables and static methods). Instead, Scala supports the concept of a singleton companion object for a class.

Scala is Strongly Typed

A type defines a set of values which a variable can possess and a set of functions that can be applied to these values. Not only is Scala strongly typed, but it has a very sophisticated type system, which is much more capable than most other strongly typed languages such as Java and C++. This means that many kinds of errors are caught by the compiler, instead of manifesting at runtime.

This also means that IDEs can offer many more features than they can when working with untyped languages such as JavaScript, Python and Ruby. However, the only IDE that actually takes advantage of this is IntelliJ IDEA by JetBrains; The Metals plugin, available for Visual Studio Code, does not offer these features. The most significant impact in on refactoring capability; Metals has cannot refactor code, while IDEA is very strong in this regard.

Scala’s type system supports the same kinds of polymorphic behavior that Java and C++ supports, but because Scala’s type system is more powerful, the polymorphic behavior is correspondingly enhanced. In case you are not familiar with polymorphic behavior, here is what Wikipedia has to say:

In programming languages, polymorphism is the provision of a single interface to entities of different types. A polymorphic type is a type whose operations can also be applied to values of some other type, or types. There are several fundamentally different kinds of polymorphism:

  • If a function denotes different and potentially heterogeneous implementations depending on a limited range of individually specified types and combinations, it is called ad-hoc polymorphism. Ad-hoc polymorphism is supported in many languages using function overloading.
  • If the code is written without mention of any specific type and thus can be used transparently with any number of new types, it is called parametric polymorphism. In the object-oriented programming community, this is often called generic programming.
  • In object-oriented programming, subtyping or inclusion polymorphism is a concept wherein a name may denote an instance of many different classes as long as they are related by some common superclass.
 – Wikipedia

Scala 3’s support for ad-hoc polymorphism is completely incompatible with Scala 2. These courses will not discuss this feature.

Interaction between parametric polymorphism and subtype polymorphism leads to the concepts of variance and bounded quantification.

One of great features of Scala’s type system is type inferencing. If the compiler can infer a type from the context of the source code, then you don’t need to explicitly specify it. This makes the code more succinct and easier to write.

This course and the next course (Intermediate Scala) explore all of these concepts in detail.

Scala supports Functional Programming

You can just use Scala as a better Java, and there would be significant benefit. However, if you also learn how to write functional code using Scala, your program could readily take advantage of the multi-core capabilities of todays computing platforms.

Functional programs are also easier to reason about than stateful programs, and are able to scale horizontally more easily. Functional programming makes writing multi-threaded and asynchronous code much easier and safer, enabling effective use of parallel collections, futures and actors.

Scala’s class library takes inspiration from functional programming to create simple control abstractions for collections that makes programming much easier and less error-prone. This course will give you only a small taste of Scala’s functional programming capabilities - the Intermediate Scala course delves into this aspect quite deeply.

No More Null Pointer Exceptions!

Uninitialized (null) objects are the source of many errors. Scala’s Option type is a solution to this problem. Option is a wrapper for a value or a lack of a value, which helps avoid null pointer exceptions. When it has a value the Some subclass is used, otherwise the None subclass is used. We will cover this concept in detail in the Option, Some and None lecture.

Scala is Succinct

Scala does away with unnecessary punctuation, rather like Ruby. For example, semicolons at the end of expressions are usually only necessary if you want to write more than one expression per line. Scala 3 took this further; while Scala 2 syntax still works fine with Scala 3, Scala 3 adopted some Python syntax conventions. This aspect of Scala 2 to Scala 3 migration has been smooth.

Expressions that span several lines are detected by the compiler, although sometimes you have to let the compiler know that the expression ends on a line by providing a semicolon.

Class definitions are also much shorter, combining getters, setters, and the primary constructor in the class definition.

Extractors and Pattern Matching

Part of learning to think in Scala is learning how to use Scala’s powerful pattern matching features. Scala provides many types of containers in its runtime library. Pattern matching allows you to extract values, if present from those containers, and to handle cases where values do not exist. This feature is introduced in this course, and is covered in much more detail in the Intermediate Scala course.

Scala Scales Horizontally

Scala handles multicore programming with ease. For example, you can convert a series of transformations of a collection into a multicore operation by adding the four characters .par to an existing expression. The data for the map/reduce paradigm is automatically parceled out into units of work across multiple CPU cores, and the results are also gathered in parallel.

Asynchronous programming in Scala does not require complex callbacks. The Intermediate Scala course following this one shows how to work with the most popular multicore programming options that Scala provides.

Scala is a Rich and Deep Language

This does mean Scala can take more time to learn than other languages, and for this reason Scala has been criticized as being complex. The truth is that Scala has support for a wider set of programming idioms and techniques than most other languages, which can make you more effective, and allows you to write higher quality code. The learning curve is worth it!

Scala has borrowed core concepts from other languages.

  • Its object-oriented features use Java as the departure point, so it easily interfaces to existing Java code. Scala borrows its handling of collections and control structures from Ruby, Lisp and Haskell.
  • Its functional programming features bear a resemblance to Ruby, Lisp and Haskell.
  • Scala supports multiple concurrency models.
  • Haskell inspires Scala’s type system.
  • The features that allow Scala’s interactive use as a scripting language resemble Ruby and Python.

Scala Has a Great Open-source Ecosystem

Scala is open source. As it matured, it spawned a large community of supporters, who have developed a wide range of third-party libraries and components.

  • Scala has some great testing tools like ScalaTest, Specs2, and ScalaCheck. ScalaCheck automatically generates a large number of test cases, based on boundary conditions and invariants. In fact, its not uncommon for an organization’s initial use of Scala to be for writing automated tests of an application written in some other JVM-based language.
  • Akka is a framework for building concurrent & distributed applications, and provides support for several concurrency models.
  • There are lots of impressive options for web frameworks. The most commonly known is the Play Framework, which is a full stack framework. Scalatra is a minimal web framework in the spirit of Ruby’s Sinatra. Akka HTTP is a minimal web framework for building streaming HTTP applications.
  • Scala has a wide range of libraries and frameworks for interfacing with persistent data stores, including SQL, and NoSQL and Big Data data stores.
  • Scala has become the most popular language for big data applications, using the Spark framework, which is partially written in Scala.
  • Scala is a great choice for writing Domain Specific Languages (DSLs). Scala has built-in syntax support for DSL languages, as well a rich parsing framework and library.
  • Scala can be even be compiled to Javascript, with Scala.js so you can use Scala to write your front-end and your back-end!
  • There are hundreds of supporting libraries and frameworks written in Scala, and thousands of existing Java libraries that you can use with Scala. Visit Scaladex and see for yourself!

... And Much More!

Some of the most interesting features are difficult to express in a sound bite. I’ll save those discussions for the course.

For those who want to know all the technical details, the Scala Specification (v2.11,, v2.12,, v2.13 is the authorative reference, and has syntax highlighting, linked headers, and a sidebar with a table of contents.

Sample Code

Students will see a tab on the Introduction to Scala course overview page entitled Details that provides the URL for the sample code repository. I show code throughout this course that can be pasted into the Scala REPL (and I show you how to work with the REPL). That code is also provided in the the courseNotes directory. You can choose to run the sample code as standalone programs or you can paste it into the REPL to play with it or you can paste it into a Scala worksheet.

Rob Pike’s 5 Rules of Programming

Rob Pike, one of the authors of Unix, the Go language and UTF-8 stated these well-known Rules of Programming. They apply to all Scala programs:

  1. You can’t tell where a program is going to spend its time. Bottlenecks occur in surprising places, so don’t try to second guess and put in a speed hack until you’ve proven that’s where the bottleneck is.
  2. Measure. Don’t tune for speed until you’ve measured, and even then don’t unless one part of the code overwhelms the rest.
  3. Fancy algorithms are slow when n is small, and n is usually small. Fancy algorithms have big constants. Until you know that n is frequently going to be big, don’t get fancy. (Even if n does get big, use Rule 2 first.)
  4. Fancy algorithms are buggier than simple ones, and they’re much harder to implement. Use simple algorithms as well as simple data structures.
  5. Data dominates. If you’ve chosen the right data structures and organized things well, the algorithms will almost always be self-evident. Data structures, not algorithms, are central to programming.

Pike’s rules 1 and 2 restate Tony Hoare’s famous maxim “Premature optimization is the root of all evil.”

Pike’s rules 3 and 4 are instances of the KISS design principle. Ken Thompson rephrased rules 3 and 4 as “When in doubt, use brute force.”

Rule 5 had previously been stated by Fred Brooks in The Mythical Man-Month. Rule 5 is often rephrased as "write stupid code that uses smart objects".


* indicates a required field.

Please select the following to receive Mike Slinn’s newsletter:

You can unsubscribe at any time by clicking the link in the footer of emails.

Mike Slinn uses Mailchimp as his marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp’s privacy practices.