Override toString in a Scala set
10,485
scala> import scala.collection.mutable
import scala.collection.mutable
scala> def IntSet(c: Traversable[Int]): mutable.Set[Int] = new mutable.SetProxy[Int] {
| override val self: mutable.Set[Int] = mutable.HashSet(c.toSeq :_*)
| override def toString = mkString(",")
| }
IntSet: (c: Traversable[Int])scala.collection.mutable.Set[Int]
scala> IntSet(1 to 3)
res0: scala.collection.mutable.Set[Int] = 1,2,3
Author by
W.P. McNeill
Updated on June 04, 2022Comments
-
W.P. McNeill almost 2 years
I want to create a set of integers called
IntSet
.IntSet
is identical toSet[Int]
in every way except that itstoString
function prints the elements as comma-delimited (the same as if you calledmkString(",")
), and it has a constructor that takes aTraversable
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(",") }