2

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(",")
}
Community
  • 1
  • 1
W.P. McNeill
  • 16,336
  • 12
  • 75
  • 111

1 Answers1

8
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
Eastsun
  • 18,526
  • 6
  • 57
  • 81