- Case class
- Case object
- Trait
- Object
- Companion object
- Class
- Inheritance
- Polymorphism
- Abstraction
- Encapsulation
- Constructor
An object is a class that has exactly one instance. It is created lazily when it is referenced, like a lazy val.
object IdFactory {
private var counter = 0
def create(): Int = {
counter += 1
counter
}
}
val newId: Int = IdFactory.create()
println(newId) // 1
val newerId: Int = IdFactory.create()
println(newerId) // 2
A companion object is an object with the same name as a class or trait and is defined in the same source file as the class.
It is used to model static methods.
Call by value evaluates the function arguments before calling the function. Call by name evaluates the function first and then evaluates the arguments if need be.
def time() = {
println("Getting time in nano seconds")
System.nanoTime
}
def exec(t: Long) = {
println("Executing function")
println("Time is " + t)
}
exec(time())
def time() = {
println("Getting time in nano seconds")
System.nanoTime
}
def exec(t: => Long) = {
println("Executing function")
println("Time is " + t)
}
exec(time())
Default arguments are used when a function is called without passing all the arguments.
def log(message: String, level: String = "INFO") = println(s"$level: $message")
log("System starting") // INFO: System starting
log("User not found", "WARNING") // WARNING: User not found
val a = Array(1,2,3,4)
val b = Array(5,6,7,8)
for {
x <- a
y <- b
} println(x + " " + y)
Case classes are like regular classes with a few key differences which we will go over.
```scala
case class Person(name: String, age: Int)
val p = Person("John", 30)
println(p.name) // John
println(p.age) // 30
```
Features of case classes:
- hashCode and equals methods are implemented out of the box
- toString method is implemented out of the box
- A copy method is implemented out of the box
- A factory method is implemented out of the box
- A companion object is created with the same name as the case class
- The parameters are public val by default
- The parameters are immutable by default
Mutable and immutable collections are available in Scala.
-
Iterable
- Seq
- Set
- Map
-
Seq
- IndexedSeq
- Vector
- Range
- String
- NumericRange
- ArraySeq
- LinearSeq
- List
- LazyList
- Queue
- IndexedSeq
LinearSeq is a linked list and IndexedSeq is an array. There is a difference in performance between the two.