Defining a generic class

Generic classes take a type as a parameter within square brackets []. One convention is to use the letter A as a type parameter identifier, though any parameter name may be used. Let's see a minimal example on Scala REPL, as follows:

scala> class Stack[A] {
| private var elements: List[A] = Nil
| def push(x: A) { elements = x :: elements }
| def peek: A = elements.head
| def pop(): A = {
| val currentTop = peek
| elements = elements.tail
| currentTop
| }
| }
defined class Stack
scala>

The preceding implementation of a Stack class takes any type A as a parameter. This means the underlying list, var elements: List[A] = Nil can only store elements of type A. The procedure def push only accepts objects of type A (note: elements = x :: elements reassigns elements to a new list created by prepending x to the current elements). Let's see an example of how to use the preceding class to implement a stack:

object ScalaGenericsForStack {
def main(args: Array[String]) {
val stack = new Stack[Int]
stack.push(1)
stack.push(2)
stack.push(3)
stack.push(4)
println(stack.pop) // prints 4
println(stack.pop) // prints 3
println(stack.pop) // prints 2
println(stack.pop) // prints 1
}
}

The output is as follows:

4
3
2
1

The second use case could be implementing a linked list too. For instance, if Scala didn't have a linked-list class and you wanted to write your own, you could write the basic functionality like this:

class UsingGenericsForLinkedList[X] { // Create a user specific linked list to print heterogenous values
private class Node[X](elem: X) {
var next: Node[X] = _
override def toString = elem.toString
}

private var head: Node[X] = _

def add(elem: X) { //Add element in the linekd list
val value = new Node(elem)
value.next = head
head = value
}

private def printNodes(value: Node[X]) { // prining value of the nodes
if (value != null) {
println(value)
printNodes(value.next)
}
}
def printAll() { printNodes(head) } //print all the node values at a time
}

Now, let's see how could we use the preceding linked list implementation:

object UsingGenericsForLinkedList {
def main(args: Array[String]) {
// To create a list of integers with this class, first create an instance of it, with type Int:
val ints = new UsingGenericsForLinkedList[Int]()
// Then populate it with Int values:
ints.add(1)
ints.add(2)
ints.add(3)
ints.printAll()

// Because the class uses a generic type, you can also create a LinkedList of String:
val strings = new UsingGenericsForLinkedList[String]()
strings.add("Salman Khan")
strings.add("Xamir Khan")
strings.add("Shah Rukh Khan")
strings.printAll()

// Or any other type such as Double to use:
val doubles = new UsingGenericsForLinkedList[Double]()
doubles.add(10.50)
doubles.add(25.75)
doubles.add(12.90)
doubles.printAll()
}
}

The output is as follows:

3
2
1
Shah Rukh Khan
Aamir Khan
Salman Khan
12.9
25.75
10.5

In summary, at the basic level, creating a generic class in Scala is just like creating a generic class in Java, with the exception of the brackets. Well! So far we have gotten to know some essential features to get started with an object-oriented programming language, Scala.

Although, we have not covered some other aspects, however, we still think that you can continue working. In Chapter 1, Introduction to Scala, we discussed what the available editors for Scala are. In the next section, we will see how to set up your build environment. More specifically, three build systems, like Maven, SBT, and Gradle will be covered.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.249.174