- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Wierd type mismatch errors, whats going on here?
Thu, 2011-03-31, 21:14
Hi,
Three lines of code:
scala> var lol = List.empty[List[AnyRef]]
lol: List[List[AnyRef]] = List()
scala> var data = List(2,3,4).map {_.asInstanceOf[AnyRef]}
data: List[AnyRef] = List(2, 3, 4)
scala> lol += data
<console>:8: error: type mismatch;
found : List[AnyRef]
required: String
lol += data
^
Which is wierd, because in the above code I haven't said anything about "String"s at all.
Now, strangely, if I try to feed an actual String into "lol", I get the correct type of error:
scala> lol += "agadsf"
<console>:7: error: type mismatch;
found : java.lang.String
required: List[scala.List[AnyRef]]
lol += "agadsf"
^
What am I doing wrong here?
Thanks.
Three lines of code:
scala> var lol = List.empty[List[AnyRef]]
lol: List[List[AnyRef]] = List()
scala> var data = List(2,3,4).map {_.asInstanceOf[AnyRef]}
data: List[AnyRef] = List(2, 3, 4)
scala> lol += data
<console>:8: error: type mismatch;
found : List[AnyRef]
required: String
lol += data
^
Which is wierd, because in the above code I haven't said anything about "String"s at all.
Now, strangely, if I try to feed an actual String into "lol", I get the correct type of error:
scala> lol += "agadsf"
<console>:7: error: type mismatch;
found : java.lang.String
required: List[scala.List[AnyRef]]
lol += "agadsf"
^
What am I doing wrong here?
Thanks.
Thu, 2011-03-31, 22:17
#2
Re: Wierd type mismatch errors, whats going on here?
On Thu, Mar 31, 2011 at 2:14 PM, Pooria Mellati wrote:
> What am I doing wrong here?
List does not have a + method, The correct method is :+
lol :+= data
see: http://www.scala-lang.org/api/current/scala/collection/immutable/List.html
The + "operator" is special and best avoided except when you're dealing with numeric values or string-like values.
More here: http://stackoverflow.com/questions/2664274/how-to-unimport-string-operator-in-scala
-0xe1a