Override toString in a Scala set

混江龙づ霸主 提交于 2019-12-02 05:04:49

问题


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(",")
}

回答1:


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


来源:https://stackoverflow.com/questions/15307213/override-tostring-in-a-scala-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!