I have some question about the following code.
Code: Alles auswählen
trait Store[+A] { //1- Why [-A] is not used instead?
def +[B >: A](b: B): Store[B] //2- Why B is super type of A? if it was [-A] then would that be fine? def +[A](A: a): Store[A]
def contains(a: Any): Boolean
}
object EmptyStore extends Store[Nothing] {
def +[B](b: B): Store[B] = new LinkedListStore(b, this) //3- How this is returning Store[B], are we overriding previous + method?
def contains(b: Any) = false
}
class LinkedListStore[+A](
val v: A,
val rest: Store[A]
) extends Store[A] {
def +[B >: A](b: B): LinkedListStore[B] =
new LinkedListStore(b, this)
def contains(a: Any): Boolean =
this.v == a || (rest contains a)
}
object Main extends App {
val a: Store[Int] = EmptyStore + 1 + 2
val b: Store[Any] = a
println(b contains 1); println(b contains 3)
}