Scala type aliases (syntax, examples)

As a little note today, in Scala 2 you can declare a type alias. Typically you do this to create a simple alias for a more complex type, as you’ll see in the examples below.

Scala 3 Update: Scala 3 also has opaque types, which we wrote about here on the docs.scala-lang.org website.

Using a type alias to simplify a complex type

For example, on this page I note a good example where someone on StackOverflow first defined a type alias named Row, and then created a second type alias named Matrix as a list of rows:

type Row = List[Int]
type Matrix = List[Row]    

The type Matrix is easier to read than this:

List[List[Int]]

This is similar to a Haskell example I remember seeing, where a Word was defined as a list of characters, a Sentence was defined as a list of words, and a Paragraph was defined as a list of sentences:

type Word = List[Char]
type Sentence = List[Word]
type Paragraph = List[Sentence]

Paragraph is certainly easier to remember than List[List[List[Char]]].

Creating a more meaningful name with a type alias

You can also create a type alias to create a more meaningful name for a type. For example, many people model money/currency using a BigDecimal, so a type alias like this makes sense:

type Money = BigDecimal

You can then use the type alias Money as a normal type in your Scala classes, objects, traits, and methods, like these examples:

def getPrice(): Money = 10.50
def doublePrice(m: Money): Money = 2.0 * m

NOTE: Some developers prefer Long for currency, some prefer BigDecimal, and no doubt there are other approaches as well.

Type alias rules

There are just a few rules to know about using type aliases in Scala. The main rule to know about a type alias is that it must be defined inside a class, object, or package object.

Another “rule” to know isn’t really a rule, but a way to think about a type alias: when you use a type alias to declare a type parameter, as with these methods:

def getPrice(): Money = 10.50
                -----

def doublePrice(m: Money): Money = 2.0 * m
                   -----   -----

the type Money is literally replaced by the compiler with its equivalent type, BigDecimal, so the compiled code becomes this:

def getPrice(): BigDecimal = 10.50
def doublePrice(m: BigDecimal): BigDecimal = 2.0 * m

Decompiling type aliases to see how they work

If you’re interested in how type aliases work “under the hood,” I just created this example. First, create this source code as a file named TypeAliases.scala:

class TypeAliases {
    type Money = BigDecimal
    def getPrice: Money = 10.50
    def doublePrice(m: Money): Money = 2.0 * m
}

Next, I used Scala 2.12 to compile that Scala file to a JVM class file like this:

$ scalac TypeAliases.scala

That creates a file named TypeAliases.class.

Next, because that’s a plain JVM class file, you can disassemble it using javap like this:

$ javap TypeAliases
Compiled from "TypeAliases.scala"
public class TypeAliases {
    public scala.math.BigDecimal getPrice();
    public scala.math.BigDecimal doublePrice(scala.math.BigDecimal);
    public TypeAliases();
}

As you can see from that output, the javap command — which reads the .class file — has no information about the Money type alias! All it sees is BigDecimal.

If you’re interested in this technique, another thing you can do is run this scalac command when you compile the TypeAliases.scala file:

$ scalac -Xprint:all TypeAliases.scala
class TypeAliases extends Object {
    def getPrice(): scala.math.BigDecimal =
        math.this.BigDecimal.double2bigDecimal(10.5);
    def doublePrice(m: scala.math.BigDecimal): scala.math.BigDecimal =
        math.this.BigDecimal.double2bigDecimal(2.0).*(m);
    def <init>(): TypeAliases = {
        TypeAliases.super.<init>();
        ()
    }
}

This scalac command compiles the Scala source code file to a .class file, and also prints out the details of what it’s doing during that process. You’ll see this particular output at the end of the compilation process, and again, as shown, the Money type alias has been erased from the code.

Summary: You create a Scala type alias to make your life easier, but by the time the code is compiled into a JVM class file, that type alias is nowhere to be found.

Scala type aliases (summary)

I’ll write more about Scala type aliases when I can, but I hope this is enough information to get you started working with them.