In
Part I I talked about Scala in general to give you a feel where it belongs. Now I want to go in a bit more detail. The Object-Oriented and Functional aspects of the language will get more clear when we cover Expressions and Functions in more detail.
Let us first discuss definitions. In Scala there are a few ways to make a definition. Using the keyword
def
one can introduce a name which stands for a certain expression. For example:
def x = e
In this case the expression
e
is evaluated every time the name
x
is used. The expression itself is not evaluated yet at the time of definition. It is evaluated every time when it is used. This type of declaration is called a Function.
Scala is a pure Object Oriented language. Everything is an object. There is no such thing as a primitive type. Since everything is an object, there is no exception for numbers and functions. This is a bit strange from a Java developer perspective, but the following expression is actually a series of method calls:
1 + 2 + 3 + 4 + 5
This expression is actually the same as:
1.+(2.+(3.+(4.+(5.))))
The unusual aspects (for a Java developer) become clear in this example. First of all
1 2 3 4 5
are all objects. They have methods with identifiers
+
.
+ - *
etc. are all valid identifiers. All this means that an expression in Scala is always a series of method calls, although it might not immediately be recognizable as such. The reason why the first code fragment looks so familiar is because of the use of infix notation. Infix operators are functions which sit in between the object and the parameters. Here follows another example which might make things more clear.
val s = "Hello"
val index = s indexOf 'l'
This actually means:
val index = s.indexOf('l')
The infix notation can only work when a function has one parameter.
Let us now look to a bigger example.
import java.io._
def readFiles(path: String): List[File] = {
val location = new File(path)
if (!location.isDirectory)
List(location)
else {
for {
val dir <- location.listFiles.toList
val files <- readFiles(dir getAbsolutePath)
} yield files
}
}
This method will recursively list all files in a directory. It will take the path as a parameter. If the location is a File, then a List with the File as element will be returned. Otherwise, if the location is a directory, all elements within the directory are checked again with the same method. The
for
structure first iterates over all the entries in the directory and will return a
List
of the content of all the entries.
yield
will assemble all the results of the for loop and appends them to one big list. An interesting feature is, that the return value of this method is the value of the last executed expression, which is either
List(location)
or
for...yield files
. Both expressions have a return type:
List[File]
.
As a side feature, you see how a Java library is directly imported in Scala.
We have now talked quite a while about the
def
declaration. There are two other types of declaration:
val
and
var
.
val
is a value declaration.
val x = "Hello"
Now the value of x cannot change anymore. The behavior is actually the same as a
final
field in Java. If you want the value of
x
to change, declare it as a
var
:
var x = "Hello"
This part has now covered the declaration of functions, values and variables.