I want to create a set of integers called IntSet
. IntSet
is identical to Set[Int]
in every way except that its toString
function prints the elements as comma-delimited (the same as if you called mkString(",")
), and it has a constructor that takes a Traversable
of integers. What is the simplest way to do this?
> IntSet((1 to 3)).toString
1,2,3
I'd think there would be some one-line way to do this, but I've been fiddling around with implicit functions and extending HashSet
and I can't figure it out.
The trick is to use a proxy object. Eastsun has the answer below. Here's a slightly different version that defines a named IntSet
type and makes it immutable.
import collection.immutable.{HashSet, SetProxy}
class IntSet(values: Traversable[Int]) extends SetProxy[Int] {
override val self: Set[Int] = HashSet(values.toSeq:_*)
override def toString() = mkString(",")
}