Adding a tuple to a set does not work

前端 未结 1 803
时光取名叫无心
时光取名叫无心 2021-01-04 17:58
scala> val set = scala.collection.mutable.Set[(Int, Int)]()
set: scala.collection.mutable.Set[(Int, Int)] = Set()

scala> set += (3, 4)
:9: erro         


        
相关标签:
1条回答
  • 2021-01-04 18:28

    The issue is that it exists in the Set trait a method +(elem1: A, elem2: A, elems: A+) and the compiler is confused by it. It actually believes that you try to use this method with 2 Int parameters instead of using it with a tuple, as expected.

    You can use instead: set += (3 -> 4) or set += ((3, 4))

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