I'm attempting to convert a stack of Strings into a generic Stack .
Here is the Stack of String implementation :
class LinkedStackOfStrings {
var first : Node = _
def isEmpty : Boolean = {
first == null
}
def push(itemName : String) = {
val oldFirst = first
first = new Node(itemName , oldFirst)
}
def pop = {
first = first.next
first.itemName
}
}
class Node(val itemName : String , val next : Node) {}
Here is my Stack of Generic types implementation :
class LinkedStackGeneric[T] {
var first : NodeGeneric[T] = _
def isEmpty : Boolean = {
first == null
}
def push(itemName : T) = {
val oldFirst = first
first = new NodeGeneric(itemName , oldFirst)
}
def pop = {
first = first.next
first.itemName
}
}
class NodeGeneric[T](val itemName : T , val next : NodeGeneric[T]) {}
When I attempt to initialse my new generic class with data :
val generic = new LinkedStackGeneric
generic.push("test")
I receive this syntax error :
type mismatch; found : String("test") required: Nothing
What am I doing wrong ? Since I'm using a type parameter should I not be able to add data of any type including String to method 'push' ?