将元组添加到集合不起作用 [英] Adding a tuple to a set does not work
本文介绍了将元组添加到集合不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
scala> val set = scala.collection.mutable.Set[(Int, Int)]()
set: scala.collection.mutable.Set[(Int, Int)] = Set()
scala> set += (3, 4)
<console>:9: error: type mismatch;
found : Int(3)
required: (Int, Int)
set += (3, 4)
^
scala> set += Tuple2(3, 4)
res5: set.type = Set((3,4))
添加 (3, 4)
不起作用 - 为什么?
Adding (3, 4)
does not work - why ?
通常,(3, 4)
也表示具有两个元素的元组.
Normally, (3, 4)
also represents a tuple with two elements.
推荐答案
问题在于它存在于Set
trait a method +(elem1: A, elem2: A, elems: A+)
编译器被它弄糊涂了.它实际上认为您尝试将此方法与 2 个 Int
参数一起使用,而不是像预期的那样与元组一起使用.
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.
您可以使用:set += (3 -> 4)
或 set += ((3, 4))
这篇关于将元组添加到集合不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文