Simple subclass question

Am I wrong in thinking a SortedSet[Int] is a subclass of Set[Int]? My code is erroneous.

import scala.collection.SortedSet
def a(): Set[Int] = {
val nums = List(1,3,2)
SortedSet(nums:_*)
}

Error Message:

[error] found : scala.collection.SortedSet[Int]
[error] required: Set[Int]
[error] SortedSet(nums:_*)

API:

trait SortedSet[A] extends Set[A] with **others

Yes, in scala 2.13, Set[A] is scala.collection.immutable.Set[A], which is not a super type of scala.collection.SortedSet[A] which may be mutable.

import scala.collection.SortedSet
def main(args: Array[String]): Unit = {
val nums = List(1,3,2)
println(SortedSet(nums:_*))
}
TreeSet(1, 2, 3)
no problem

Thanks. I therefore can solve it by importing scala.collection.immutable.SortedSet

On the topic of sets, is there a way of representing a full set such that

(myFullSet: Set[Int]) intersect Set(1,2) = Set(1,2)

?

Probably not if you want to use something existing in stdlib. Sets are represented as explicit collections of items, not as predicates. You could however try to implement a set subclass that would work on predicates. Question is how you would implement the various methods existing on sets, including map, flatMap, groupBy, etc

IIUC Paul Phillips argued that intensional and extensional sets should be implemented as unrelated classes. Slide 35 at https://www.slideshare.net/extempore/a-scala-corrections-library

2 Likes