Skip to content

Instantly share code, notes, and snippets.

@xuwei-k
Last active August 10, 2016 00:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xuwei-k/0f259b6964e5a8fe35ceddd786711109 to your computer and use it in GitHub Desktop.
Save xuwei-k/0f259b6964e5a8fe35ceddd786711109 to your computer and use it in GitHub Desktop.
scala.collection.mutable.Stack.empty is awesome :p
Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_102).
Type in expressions for evaluation. Or try :help.
scala> import scala.collection.mutable
import scala.collection.mutable
scala> val a: mutable.Stack[Int] = mutable.Stack.empty
<console>:12: error: type mismatch;
found : scala.collection.mutable.Stack[Nothing]
required: scala.collection.mutable.Stack[Int]
Note: Nothing <: Int, but class Stack is invariant in type A.
You may wish to investigate a wildcard type such as `_ <: Int`. (SLS 3.2.10)
val a: mutable.Stack[Int] = mutable.Stack.empty
^
// mutable.Set is invariant. We can't use Stack.empty without asInstanceOf
scala> val a: mutable.Stack[Int] = mutable.Stack.empty.asInstanceOf[mutable.Stack[Int]]
a: scala.collection.mutable.Stack[Int] = Stack()
scala> a.push(1)
res0: a.type = Stack(1)
scala> mutable.Stack.empty // empty is not empty !!!
res1: scala.collection.mutable.Stack[Nothing] = Stack(1)
@aloiscochard
Copy link

aloiscochard commented Aug 1, 2016

@SethTisue
Copy link

I think this will be merged: scala/scala#5260

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment