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' ?
You can fix this issue by making the Stack object immutable:
object ImmutableStack {
trait ImmutableStack[+A] {
def isEmpty: Boolean
def push[B >: A](item: B): ImmutableStack[B] = new <<(this, item)
def pop: (ImmutableStack[A], Option[A])
def <<[B >: A](item: B) = push(item)
}
case class <<[+A](init: ImmutableStack[A], last: A) extends ImmutableStack[A] {
override def isEmpty = false
override def pop: (ImmutableStack[A], Option[A]) = (init, Some(last))
override def toString = "" + init + " << " + last
}
case object Nst extends ImmutableStack[Nothing] {
override def isEmpty = true
override def pop = (Nst, None)
override def toString = "Nst"
}
}
Then we have:
scala> import ImmutableStack._
import ImmutableStack._
scala> val intStack = Nst << 5 << 4 << 3 << 2 << 1
intStack: ImmutableStack.ImmutableStack[Int] = Nst << 5 << 4 << 3 << 2 << 1
scala> intStack.pop
res0: (ImmutableStack.ImmutableStack[Int], Option[Int]) = (Nst << 5 << 4 << 3 << 2,Some(1))
scala> val firstItems << lastItem = intStack
firstItems: ImmutableStack.ImmutableStack[Int] = Nst << 5 << 4 << 3 << 2
lastItem: Int = 1
scala> val strStack = Nst << "A" << "B" << "C"
strStack: ImmutableStack.ImmutableStack[String] = Nst << A << B << C
scala> val fs << lt = strStack
fs: ImmutableStack.ImmutableStack[String] = Nst << A << B
lt: String = C
scala> val anyStack = Nst << 1 << "Str" << 'Z'
anyStack: ImmutableStack.ImmutableStack[Any] = Nst << 1 << Str << Z
scala> val st << x1 << x2 << x3 = anyStack
st: ImmutableStack.ImmutableStack[Any] = Nst
x1: Any = 1
x2: Any = Str
x3: Any = Z
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With