Scala Unified Types Flashcards
Any
Definition
Any
is the supertype of all types, also called the top type. It defines certain universal methods such as equals
, hashCode
, and toString
.
Any
’s direct subsclasses
AnyVal
and AnyRef
AnyVal
Definition
AnyVal
represents value types
There are nine predefined value types and they are non-nullable.
List 9 predefined value types
Double, Float, Long, Int, Short, Byte, Char, Unit, and Boolean
AnyRef
Definition
AnyRef
represents reference types
All non-value types are defined as reference types. Every user-defined type in Scala is a subtype of AnyRef
. If Scala is used in the context of a Java runtime environment, AnyRef
corresponds to java.lang.Object
.
Type casting
How value types are casted in Scala
- Byte -> Short -> Int -> Long -> Float -> Double
- Char -> Int
Nothing
Definition
Nothing
is a subtype of all types, also called the bottom type.
There is no value that has type Nothing
. A common use is to signal non-termination such as a thrown exception, program exit, or an infinite loop (i.e., it is the type of an expression which does not evaluate to a value, or a method that does not return normally).
Null
Definition
Null
is a subtype of all reference types (i.e. any subtype of AnyRef
). It has a single value identified by the keyword literal null
.
Null
is provided mostly for interoperability with other JVM languages and should almost never be used in Scala code. We’ll cover alternatives to null later in the tour.