Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Stack[+A] {
- def push[B >: A](elem: B): Stack[B] = new Stack[B] {
- override def top: B = elem
- override def pop: Stack[B] = Stack.this
- override def toString() = elem.toString() + " " +
- Stack.this.toString()
- }
- def top: A = error("no element on stack")
- def pop: Stack[A] = error("no element on stack")
- override def toString() = ""
- }
- object VariancesTest extends Application {
- var s: Stack[Any] = new Stack().push("hello");
- s = s.push(new Object())
- s = s.push(7)
- Console.println(s)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement