scala – 将元组附加到元组的MutableList会产生错误
发布时间:2020-12-16 09:07:19 所属栏目:安全 来源:网络整理
导读:scala val x = mutable.MutableList[(Int,Int)]()x: scala.collection.mutable.MutableList[(Int,Int)] = MutableList()scala x += (1,2)console:10: error: type mismatch;found : Int(1)required: (Int,Int) x += (1,2) ^ 解决方法 它正在解释它,好像你试
scala> val x = mutable.MutableList[(Int,Int)]() x: scala.collection.mutable.MutableList[(Int,Int)] = MutableList() scala> x += (1,2) <console>:10: error: type mismatch; found : Int(1) required: (Int,Int) x += (1,2) ^ 解决方法
它正在解释它,好像你试图用2个参数调用=方法(而不是一个元组参数).
请尝试以下之一 x += ((1,2)) val t = (1,2) x += t x += 1 -> 2 //syntactic sugar for new tuple 请注意,编译器无法确定您是否尝试使用单个元组参数调用该方法,因为有多个=重载: def +=(elem : A) : Growable.this.type def +=(elem1 : A,elem2 : A,elems : A*) : Growable.this.type 如果没有第二次重载,编译器就可以解决它.以下示例可能会澄清: class Foo { def bar(elem: (Int,Int)) = () def baz(elem: (Int,Int)) = () def baz(elem1: (Int,Int),elem2: (Int,Int)) = () } 以您调用的方式调用bar =现在有效,但有警告: foo.bar(2,3) //No confounding overloads,so the compiler can figure out that we meant to use a tuple here warning: Adapting argument list by creating a 2-tuple: this may not be what you want. 以你调用的方式调用baz =失败,出现类似的错误: f.baz(3,4) error: type mismatch; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |