OK, so Set is invariant in its type parameter and works exactly the same as
scala> Set(1, 2, 3) contains "Hi" <console>:8: error: type mismatch; found : java.lang.String("Hi") required: Int Set(1, 2, 3) contains "Hi" ^
But as you say:
scala> Map('a -> 1, 'b -> 2, 'c -> 3).values.toSet contains "Hi" res1: Boolean = false
The only conclusion we can come to is that the type of Set not Set[Int] . What happens if we explicitly say scala, we want Set[Int] ? The same piece of code with an explicit parameter type works just fine (i.e., it does not compile):
scala> Map('a -> 1, 'b -> 2, 'c -> 3).values.toSet[Int] contains "Hi" <console>:8: error: type mismatch; found : java.lang.String("Hi") required: Int Map('a -> 1, 'b -> 2, 'c -> 3).values.toSet[Int] contains "Hi" ^
The problem is that the parameter of the intended type is passed to the toSet method. scala obviously takes into account contains "Hi" and prints lub Int and String (i.e. Any )
oxbow_lakes
source share