How to override Scala's default numeric type when defining a field

Scala FAQ: How do I override the default numeric type that Scala assigns when I declare an Int, Long, Short, Float, or Double val or var field?

Back to top

Solution

If you assign 1 to a variable, Scala assigns it the type Int:

scala> val a = 1
a: Int = 1

Usually this is a good thing, but if you want something else, like a Long,Short, etc., you need to tell Scala what numeric type you want. The following examples show one way to override simple numeric types:

scala> val a = 1d
a: Double = 1.0

scala> val a = 1f
a: Float = 1.0

scala> val a = 1000L
a: Long = 1000

Another approach is to annotate the variable with a type, like this:

scala> val a = 0: Byte
a: Byte = 0

scala> val a = 0: Int
a: Int = 0

scala> val a = 0: Short
a: Short = 0

scala> val a = 0: Double
a: Double = 0.0

scala> val a = 0: Float
a: Float = 0.0

Spacing after the colon isn’t important, so you can use this format, if preferred:

val a = 0:Byte

According to the Scala Style Guide, those examples show the preferred style for annotating types, but I personally prefer the following syntax when assigning types to variables, specifying the type after the variable name:

scala> val a:Byte = 0
a: Byte = 0

scala> val a:Int = 0
a: Int = 0

Hex values

You can create hex values by preceding the number with a leading 0x or 0X, and you can store them as an Int or Long:

scala> val a = 0x20
a: Int = 32

// if you want to store the value as a Long
scala> val a = 0x20L
a: Long = 32

In some rare instances, you may need to take advantage of type ascription. A post on Stack Overflow shows a case where it’s advantageous to upcast a String to an Object. The technique is shown here:

scala> val s = "Dave"
s: String = Dave

scala> val p = s: Object
p: Object = Dave

As you can see, the technique is similar to this recipe. This upcasting is known as type ascription. The official Scala documentation describes type ascription as follows:

Ascription is basically just an up-cast performed at compile time for the sake of the type checker. Its use is not common, but it does happen on occasion. The most often seen case of ascription is invoking a varargs method with a single Seq parameter.

Back to top

Discussion

It’s helpful to know about this approach when creating object instances. The general syntax looks like this:

// general case
var [name]:[Type] = [initial value]

// example
var a:Short = 0

This form can be helpful when you need to initialize numeric var fields in a class:

class Foo {
    var a: Short = 0     // specify a default value
    var b: Short = _     // defaults to 0
}

As shown, you can use the underscore character as a placeholder when assigning an initial value. This works when creating class variables, but doesn’t work in other places, such as inside a method. For numeric types this isn’t an issue -- you can just assign the type the value zero -- but with most other types, you can use this approach inside a method:

var name = null.asInstanceOf[String]

Better yet, use the Option/Some/None pattern. It helps eliminate null values from your code, which is a very good thing. You’ll see this pattern used in the best Scala libraries and frameworks, such as the Play Framework. An example of this approach is shown in Recipe 12.4 of the Scala Cookbook, “How to Process Every Character in a Text File.”

Back to top

See Also

  • See Recipe 20.5 of the Scala Cookbook, “Eliminate null Values from Your Code,” and Recipe 20.6, “Using the Option/Some/None Pattern,” for more discussion of this topic.
  • The Scala Style Guide
Back to top

The Scala Cookbook

This tutorial is sponsored by the Scala Cookbook, which I wrote for O’Reilly:

You can find the Scala Cookbook at these locations:

Back to top

Add new comment

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.