Scala/Basic types

Static typing
Scala is a statically typed language. This means that when Scala programs are compiled, the compiler verifies that the operations on objects are valid according to their types, and if they are not, a message regarding the type error is given. A simple example of a program that is rejected due to type errors:

The string method "*" is not defined for real numbers, so when the compiler is compiling the program, it fails to find any version of the "*" method on strings that takes a real number as argument, and it therefore outputs an error message. The error message depends on the compiler and its version, but generally includes the position, the specific type error, and other useful information.

Fixing the program by giving an integer value gives us the following program:

Since the string method "*" is defined for integers, the compiler verifies that the types are valid, and accepts the program.

Type annotations and type inference
The type of values, variables and expressions can be indicated by using type annotations:

In the first line, we declare a value "someInteger" that is assigned the expression "3:Int". The colon followed by "Int" is a type annotation that specifies that 3 is of type "Int". The compiler verifies that this is valid during type-checking. In the second line, we declare a value "someReal" that is assigned the expression "4:Double". While "4" is normally interpreted as an "Int", the type annotation means that it is instead interpreted as a "Double". In the third line, we declare a value "someString" that we assign the expression "5:String". However, "5" cannot be interpreted as a "String", and the type-checking fails with an error message from the compiler.

In the above example, the type annotation for "3" was not strictly required. Type annotations are often optional, and can be placed multiple places:

The reason type annotations are often not required is because Scala supports type inference, which means that it can often infer the types based on the different elements. Type annotations can be used as a guide to the type inferrer, for instance when it is having difficulty inferring a type and or when it has inferred the wrong type.

Examples of places where type annotations are required include the arguments of function definitions and the return types of recursive functions.

Boolean
The "Boolean" has only two possible values: true and false. The keywords "true" and "false" are the literals for "Boolean":

There are several operators on boolean values. The most common unary operator is complement or negation, which in Scala is implemented by the "!" method:

Two of the most common binary operators are conjunction, or "and", and disjunction, or "inclusive or". In Scala, conjunction and disjunction are implemented by respectively the "&&" and "||" methods:

Other binary operators include equal (implemented by "=="), not equal (implemented by "!=") and exclusive or (implemented by "^").

The Boolean type is often useful for making decisions. A simple example is the if-then-else expression that uses an expression of type Boolean to decide which branch to evaluate:

Integral
The integral types describe numbers that are discrete, ie. they do not have any fractions. The different integral types vary in the range of numbers they can represent. They include "Byte", "Short", "Int", "Long", "BigInt" and "Char". The below table describes the range of discrete numbers each type can represent:

Byte, Short, Int and Long
"Byte", "Short", "Int" and "Long" are used to represent numbers or data. They support simple arithmetic operators such as negation, addition, minus, multiplication, division and modulus:

Note that arithmetic operations involving "Byte", "Short" and "Int" but not "Long" are converted to "Int", while operations involving "Long" and any of the other types are converted to "Long". Modulus can give negative values, and division and modulus are designed such that the equality "b * (a/b) + (a%b) == a" generally holds, even for negative "a" and "b". The second argument to division or modulus may not be 0:

Relational operators are also supported, such as less-than, less-than-or-equal, equal and not-equal:

Other relational operators include greater-than and greater-than-or-equal, which are implemented by ">" and ">=" respectively.

Bitwise operators include "~", "&", "|" and "^", and bit-shift operators include "<<", ">>" and ">>>".

Literals can be declared by writing out the number. Alternative notations include octal and hexidecimal:

In the first line, "10" is printed. In the second line, we write the octal number "10" by prepending the number with "0", and "8" is printed, which is the corresponding decimal number. In the third lin,e we write the hexadecimal number "10" by prepending the number with "0x", and "16" is printed, which is the corresponding hexadecimal number. The fourth line is like the third line, except "0X" is used to prepend with instead of "0x". Note that numbers are always printed in decimal notation.

Apart from the prefix "0x" or "0X", hexadecimal literals consists of normal digits as well as the letters "a" through "f". There is no difference between lower case or upper case:

"Long" literals are indicated by appending the number with "l" or "L":

Integer overflows and underflows are what happens when somewhere in an integral expression, the number calculated becomes too large or too small for the integral type to hold:

In the first line, the maximum number for "Int" was printed. In the second line, the maximum number for "Int" plus one was printed, wrongly giving the minimum number for "Int". In the third line, the minimum for "Int" minus one was printed, wrongly giving the maximum number for "Int". Underflows and overflows do not generally cause an exception, but results in the wrong result.

The normal procedure to avoid overflows and underflows is to ensure that the integral type used is always large enough to hold the expression. This must be done at every step in the computation:

In the first line, the integer literals in the expression are interpreted as "Int". Because "Int" is not big enough to contain all the intermediate results, overflows happen and give the wrong result. After the wrong result is computed, it is converted to "Long" and stored in the value "wrongResult". In the second line "wrongResult" is printed, giving the wrong number "500654080". In the third line, the first integer literal is indicated to be of type "Long". Since operators are left-associative, and expressions involving "Int" and "Long" has the type "Long", the type for the intermediate results is "Long", and is big enough to contain the resulting numbers, with no overflows happening. The result is stored in the value "rightResult". In the fourth line, "rightResult" is printed, giving the right number "86400000000".

BigInt
BigInt supports arbitrary precision, meaning that it can represent all discrete numbers given enough memory is available:

In the first line, we try to declare a number using a "Long" literal, but the number is too big to be held in a "Long", and the compiler gives an error. In the second line, we declare the same number using a string, and assign it to the value "someNum". In the third line, we print the value "someNum". In the fourth line, we perform basic arithmetic with someNum and an integer literal of type "Int", and print the result.

The operations and memory use of "BigInt" is dependent on the size of the number it is representing, and generally performs slower than the other integral types. Since it has arbitrary precision, computation done purely using "BigInt" never results in integer overflow or underflow.

Char
"Char" is used first and foremost for representing characters in strings.

TODO: Expand this section.

Special types
TODO: Write something about "Unit", "Any", "Nothing", etc. Maybe also "null". TODO: Discuss associativity.