Tuesday, April 3, 2012

Scala - Type System



Scala inherited all built-in types from Java, such as String, Int, Char, Boolean. 
In fact, only a few new built-in data types are introduced. For instance, Scala’s Boolean type corresponds to Java’s Boolean type class. 
One interesting data type is ‘unit’. ‘Unit’ basically means nothing specially. If a function has a return value in type ‘unit’, it indicates that it is returning nothing, like the ‘void’ type in Java. Scala’s compiler is able to convert any return type to be a ‘unit’, to avoid this from happening, an equal sign ‘=’ should always be inserted after the function is declared. When any data type is converted to a ‘unit’ by the complier, its value will be lost. 
In the book Programming Scala, Dean Wampler states that “Scala allows you to decide whether a variable is immutable (read-only) or not (read-write) when you declare it.” 
Therefore, types do not need to be declared for neither immuatables nor mutables data; Scala is able to figure out which type in run- time. 

No comments:

Post a Comment