Override toString in a Scala set

后端 未结 1 1098
伪装坚强ぢ
伪装坚强ぢ 2021-01-21 14:39

I want to create a set of integers called IntSet. IntSet is identical to Set[Int] in every way except that its toString funct

1条回答
  •  悲哀的现实
    2021-01-21 14:59

    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
    

    0 讨论(0)
提交回复
热议问题