How do I specify a newBuilder for a scala set?

后端 未结 1 1787
独厮守ぢ
独厮守ぢ 2020-12-11 10:59

I am trying to extend a set of integers in Scala. Based on an earlier answer I have decided to use a SetProxy object. I am now trying to implement the newBuilder

相关标签:
1条回答
  • 2020-12-11 11:25

    Give this a shot:

    case class CustomSet(override val self: Set[Int]) extends SetProxy[Int] {
      override def newBuilder = new mutable.SetBuilder[Int, Set[Int]](CustomSet())
    }
    
    object CustomSet {
      def apply(values: Int*): CustomSet = CustomSet(HashSet(values.toSeq: _*))
    }
    

    When creating the SetBuilder, specifying CustomSet as the second type param did not satisfy the type bound for that param. Switching it to Set[Int] meets that criteria and allows you to still pass in your CustomSet as the constructor arg. Hope this helps.

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