Why are Map and Set smoothed in scala.Predef?

9 times out of 10, just using Map and Set behave as I expected, but sometimes I am suddenly struck

 error: type mismatch; [INFO] found : scala.collection.Set[String] [INFO] required: Set[String] 

As an example, from REPL:

 scala> case class Calculator[+T](name: String, parameters: Set[String]) defined class Calculator scala> val binding=Map.empty[String, String] binding: scala.collection.immutable.Map[String,String] = Map() scala> Calculator("Hello",binding.keySet) <console>:9: error: type mismatch; found : scala.collection.Set[String] required: Set[String] Calculator("Hello",binding.keySet) ^ 

I think I understand the error, that is, a function call for aliases returns the actual types.

And so it seems to me that the solution is to import non-smoothed types. After that, every other file in my project will now generate type mismatch errors, so I will have to import it into each file. Which leads to the question I ask in the title - what was the purpose of the alias in Predef if I ultimately need to import the actual package?

Is my understanding wrong, or my use case is not typical, or both?

+7
scala
source share
2 answers

You have identified the problem incorrectly. It is not that he does not recognize an alias of the type of the same type as his aliases. This is what an alias of type scala.collection.immutable.Set is and it is not the same as scala.collection.Set.

Editing: by the way, I thought I fixed it, as can be seen from the comment in the type diagnostics:

  ... Also, if the * type error is because of a conflict between two identically named * classes and one is in package scala, fully qualify the name so one * need not deduce why "java.util.Iterator" and "Iterator" don't match. 

Apparently more work is needed.

Edit 7/17/2010: OK, it took me quite a while, but now, at least, it says something incomprehensible.

 files/neg/type-diagnostics.scala:4: error: type mismatch; found : scala.collection.Set[String] required: scala.collection.immutable.Set[String] def f = Calculator("Hello",binding.keySet) ^ 
+11
source share

The real problem is that scala.collection.immutable.Map#keySet returns scala.collection.Set (read-only Set ) instead of scala.collection.immutable.Set (immutable Set ). I will leave it for someone else to explain why this ...

Edit

Someone asks for an explanation such as returning Map#keySet to this stream , but is not receiving a response.

+2
source share

All Articles