This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Re: Map and Set interoperability

1 reply
Avi Pfeffer 2
Joined: 2009-02-25,
User offline. Last seen 42 years 45 weeks ago.

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.

Naftoli Gugenheim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: Map and Set interoperability
Predef imports scala.collection.immutable.Set, IIRC, so unqualified Set is a subclass of the Set specified by some APIs.הצלחה!

On Fri, Mar 13, 2009 at 1:48 PM, Avi Pfeffer <avi.pfeffer@gmail.com> wrote:
On Fri, Mar 13, 2009 at 1:32 PM, Randall R Schulz <rschulz@sonic.net> 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.

>> <console>: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.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland