Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This was a conscious design decision to make Set.contains/apply typesafe. You should not be able to have a s:Set[Int] and accidentally do something like s.contains("x") which would always be false so probably not what you intended. Also, Set[T] implements Function[T,Boolean], which is only possible if the apply method does not take an Any.</p> <p>There were endless discussions about this topic on the scala-user mailing list. See for example <a href="https://groups.google.com/forum/#!topic/scala-user/hbxt2TnRii0" rel="nofollow">this</a> or <a href="http://grokbase.com/t/gg/scala-language/1286jy8xce/collections-annoyances-missed-opportunities-for-structural-sharing" rel="nofollow">this</a>.</p> <p>Here is a quote from Paul Phillips from the second discussion that summarizes the rationale very well:</p> <p>"Yes, apply (aliased to contains) is the central operation of Set, whereas contains is indeed "just some method" on Seq.</p> <p>Nobody's saying you don't sometimes want a covariant Set, but on balance, it is more useful invariant."</p> <p>Note that you can always add covariance to sets with an implicit conversion. This means that if you have e.g. a Set[Int] and a method that takes a Set[Any], it will work. But it also means that you can now accidentally call Set[Int].contains("x"), and the compiler will not catch the error (you will always get false).</p> <pre><code>scala&gt; implicit def setIsCovariant[T,U &lt;: T](s:Set[U]):Set[T] = s.asInstanceOf[Set[T]] setIsCovariant: [T, U &lt;: T](s: Set[U])Set[T] scala&gt; val s : Set[Int] = Set(1,2,3,4) s: Set[Int] = Set(1, 2, 3, 4) scala&gt; s.contains("x") res0: Boolean = false scala&gt; val a: Set[Any] = s a: Set[Any] = Set(1, 2, 3, 4) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload