- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Re: Map and Set interoperability
Fri, 2009-03-13, 18:48
On Fri, Mar 13, 2009 at 1:32 PM, Randall R Schulz wrote:
> On Friday March 13 2009, Avi Pfeffer wrote:
>> Hi all,
>>
>> I was surprised to see the following:
>>
>> ...
>>
>> scala> val m = Map("a" -> 1, "b" -> 2)
>> m: scala.collection.immutable.Map[java.lang.String,Int] = Map(a -> 1,
>> b -> 2)
>>
>> scala> m.keySet: Set[String]
>
> What are you trying to do here? m.keySet retrieves the key set for the
> Map m, and that works as you'd expect:
>
> scala> val m = Map("a" -> 1, "b" -> 2)
> m: scala.collection.immutable.Map[java.lang.String,Int] = Map(a -> 1, b -> 2)
>
> scala> m.keySet
> res0: scala.collection.Set[java.lang.String] = Set(a, b)
Yes, that is what I'm trying to do, but it returns it as a
scala.collection.Set rather than a scala.collection.immutable.Set, so
when I say
m.keySet: Set[String]
it complains.
>> :6: error: type mismatch;
>> found : scala.collection.Set[java.lang.String]
>> required: Set[String]
>> m.keySet: Set[String]
>> ^
At least that's what I think is happening.
On Fri, Mar 13, 2009 at 1:48 PM, Avi Pfeffer <avi.pfeffer@gmail.com> wrote: