Last active
August 10, 2016 00:49
-
-
Save xuwei-k/0f259b6964e5a8fe35ceddd786711109 to your computer and use it in GitHub Desktop.
scala.collection.mutable.Stack.empty is awesome :p
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment