val lines = scala.io.Source.fromFile("file.txt").mkString
By the way, "scala.
" isn't really necessary, as it's always in scope anyway, and you can, of course, import io's contents, fully or partially, and avoid having to prepend "io." too.
The above leaves the file open, however. To avoid problems, you should close it like this:
val source = scala.io.Source.fromFile("file.txt")
val lines = try source.mkString finally source.close()
Another problem with the code above is that it is horrible slow due to its implementation nature. For larger files one should use:
source.getLines mkString "\n"
tl;dr
class C
defines a class, just as in Java or C++.
object O
creates a singleton object O
as instance of some anonymous class; it can be used to hold static members that are not associated with instances of some class.
object O extends T
makes the object O
an instance of trait T
; you can then pass O
anywhere, a T
is expected.
- if there is a
class C
, then object C
is the companion object of class C
; note that the companion object is not automatically an instance of C
.
Also see Scala documentation for object and class.
object
as host of static members
Most often, you need an object
to hold methods and values/variables that shall be available without having to first instantiate an instance of some class.
This use is closely related to static
members in Java.
object A {
def twice(i: Int): Int = 2*i
}
You can then call above method using A.twice(2)
.
If twice
were a member of some class A
, then you would need to make an instance first:
class A() {
def twice(i: Int): Int = 2 * i
}
val a = new A()
a.twice(2)
You can see how redundant this is, as twice
does not require any instance-specific data.
object
as a special named instance
You can also use the object
itself as some special instance of a class or trait.
When you do this, your object needs to extend some trait
in order to become an instance of a subclass of it.
Consider the following code:
object A extends B with C {
...
}
This declaration first declares an anonymous (inaccessible) class that extends both B
and C
, and instantiates a single instance of this class named A
.
This means A
can be passed to functions expecting objects of type B
or C
, or B with C
.
Additional Features of object
There also exist some special features of objects in Scala.
I recommend to read the official documentation.
def apply(...)
enables the usual method name-less syntax of A(...)
def unapply(...)
allows to create custom pattern matching extractors
- if accompanying a class of the same name, the object assumes a special role when resolving implicit parameters
Best Answer
Mathematicians have their own little funny ways, so instead of saying "then we call function
f
passing itx
as a parameter" as we programmers would say, they talk about "applying functionf
to its argumentx
".apply
serves the purpose of closing the gap between Object-Oriented and Functional paradigms in Scala. Every function in Scala can be represented as an object. Every function also has an OO type: for instance, a function that takes anInt
parameter and returns anInt
will have OO type ofFunction1[Int,Int]
.Since everything is an object in Scala
f
can now be treated as a reference toFunction1[Int,Int]
object. For example, we can calltoString
method inherited fromAny
, that would have been impossible for a pure function, because functions don't have methods:Or we could define another
Function1[Int,Int]
object by callingcompose
method onf
and chaining two different functions together:Now if we want to actually execute the function, or as mathematician say "apply a function to its arguments" we would call the
apply
method on theFunction1[Int,Int]
object:Writing
f.apply(args)
every time you want to execute a function represented as an object is the Object-Oriented way, but would add a lot of clutter to the code without adding much additional information and it would be nice to be able to use more standard notation, such asf(args)
. That's where Scala compiler steps in and whenever we have a referencef
to a function object and writef (args)
to apply arguments to the represented function the compiler silently expandsf (args)
to the object method callf.apply (args)
.Every function in Scala can be treated as an object and it works the other way too - every object can be treated as a function, provided it has the
apply
method. Such objects can be used in the function notation:There are many usage cases when we would want to treat an object as a function. The most common scenario is a factory pattern. Instead of adding clutter to the code using a factory method we can
apply
object to a set of arguments to create a new instance of an associated class:So
apply
method is just a handy way of closing the gap between functions and objects in Scala.