Scala: value :: 不是 Int 的成员
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24681777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scala: value :: is not a member of Int
提问by paK0
I recently started using scala and I can't make anything of the error messages. For the following code I get the stated message(using eclipse):
我最近开始使用 Scala,但我无法制作任何错误消息。对于以下代码,我得到了规定的消息(使用 eclipse):
def helper: Int => List[Int] = x => x match {
case 2 => 2::1
...
}
I can fix it by using List(2,1), but souldn't that be the same thing as 2::1? I have similar problems where the List(...) approach would be harder to use, so I really want to know where my thinking mistake is.
我可以使用 List(2,1) 来修复它,但这与 2::1 不一样吗?我有类似的问题,其中 List(...) 方法更难使用,所以我真的很想知道我的思维错误在哪里。
回答by sepp2k
Infix operators are interpreted as method calls in Scala. If the infix operators ends with a colon, it's a method call on the right operand with the left operand as its argument. Otherwise it's a method call on the left operand with the right operand as its argument.
中缀运算符在 Scala 中被解释为方法调用。如果中缀运算符以冒号结尾,则它是对右操作数的方法调用,左操作数作为其参数。否则,它是对左操作数的方法调用,右操作数作为其参数。
In other words, if you do x + y, it's the same as x.+(y), i.e. you're calling the method +on the object x, with yas the argument. And if you do x :: yit's the same as y.::(x), calling the method ::on the object y.
换句话说,如果你这样做x + y,它与 相同x.+(y),即你正在调用+对象上的方法x,y作为参数。如果你做的x :: y是一样的y.::(x),调用该方法::的对象y。
So in your example you're calling the method ::on the object 1, which is an Int. However the class Intdoes not have a ::method, so this does not work and you get an error message telling you that the ::method does not exist for the Intclass.
因此,在您的示例中,您正在调用::object 上的方法,该方法1是Int. 但是,该类Int没有::方法,因此这不起作用,并且您会收到一条错误消息,告诉您该类::不存在该方法Int。
To make ::work, the right operand needs to be a list (or something else that has a ::method), so 2 :: 1 :: Nilwould work. However in this case using List()seems like the cleaner alternative.
为了::工作,正确的操作数需要是一个列表(或其他有::方法的东西),这样2 :: 1 :: Nil才能工作。然而,在这种情况下,使用List()似乎是更清洁的选择。
回答by godfatherofpolka
The expression 2::1is interpreted in Scala as:
该表达式2::1在 Scala 中被解释为:
{ val x = 2; 1.::(x) }
because operators ending in a colon :are right-associative and if opis right-associative, then e1 op e2is interpreted as { val x = e1; e2.op(x) }(see Scala Language Reference, Section 6.12.3, p. 84, which is p. 92 of the PDF).
因为以冒号结尾的运算符:是右结合的,如果op是右结合,则e1 op e2解释为{ val x = e1; e2.op(x) }(参见Scala 语言参考,第 6.12.3 节,第 84 页,即 PDF 的第 92 页)。
For the purposes here, basically the following simplified version is called
出于此处的目的,基本上称为以下简化版本
1.::(2)
However, 1is of type Intand Intdoes not have a method with name ::(and there is also no implicit conversion to another type that has such a method), hence the error.
但是,1是类型Int并且Int没有带名称的方法::(并且也没有隐式转换为具有这种方法的另一种类型),因此出现错误。
As ayvango has pointed out above, you could use
正如ayvango在上面指出的那样,您可以使用
2::1::Nil
which is interpreted as
这被解释为
Nil.::(2).::(1)
Now this works perfectly well, because Nilis of type List[Nothing]and does have a method ::, see scala.collection.immutable.ListFurthermore, ::returns something of type List[Int], so the subsequent call .::(1)is also fine.
现在这工作得很好,因为Nil是 typeList[Nothing]并且确实有一个 method ::,请参阅scala.collection.immutable.List此外,::返回 type 的东西List[Int],因此后续调用.::(1)也可以。
Another way is
另一种方式是
2::List(1)
which becomes List(1).::(2)and works for the same reason as above.
这List(1).::(2)与上述原因相同并起作用。
Your confusion might be due to the fact that you consider List(2,1)to be the same as 2::1, however, it is actually 2::1::Nil. Think of lists as being built inductively as follows:
您的困惑可能是由于您认为List(2,1)与 相同2::1,但实际上它是2::1::Nil。将列表视为以归纳方式构建的,如下所示:
Nilis a list- if
headis an element andtailis a list, thenhead::tailis a list
Nil是一个列表- 如果
head是一个元素并且tail是一个列表,那么head::tail就是一个列表
as witnessed by the implementation of lists (simplified version, omitting traits)
正如列表的实现所见证的那样(简化版,省略特征)
sealed abstract class List[+A]
final case class ::[B](head: B, tl: List[B]) extends List[B]
object Nil extends List[Nothing]
Thus, lists always "end with" Nilin the ::form of presentation.
因此,名单总是“结束与”Nil在::演示文稿的形式。
On a sidenote, you could also try to automatically wrap Intinto List[Int]by using something like
在阿里纳斯,你也可以尝试自动换Int到List[Int]使用类似
implicit def wrap(x : Int) : List[Int] = List(x)
or use similar functionalities provided by libraries such as Scalazbut this might not always be desirable and probably is a bit beyond the scope of this question.
或使用Scalaz等库提供的类似功能,但这可能并不总是可取的,并且可能有点超出了这个问题的范围。
回答by Venkat Sudheer Reddy Aedama
:: is a method defined on List. You are trying to use this operator with Int type. Take a look at: http://www.scala-lang.org/api/current/index.html#scala.collection.immutable.List
:: 是在 List 上定义的方法。您正在尝试将此运算符与 Int 类型一起使用。看看:http: //www.scala-lang.org/api/current/index.html#scala.collection.immutable.List
As @ayvango pointed out, you could do:
正如@ayvango 指出的,你可以这样做:
def helper: Int => List[Int] = x => x match {
case 2 => 2 :: 1 :: Nil
}
回答by Alireza
If you want to use ::operator, on Intor other types like string, just add Nilto the end of the list, otherwise you get this error:
如果你想使用::operator、onInt或其他类型,如string,只需将Nil添加到列表的末尾,否则会出现此错误:
value :: is not a member of Int
value :: 不是 Int 的成员
this won'tbe compiled:
这不会被编译:
println(0::1::2::3::4::5)
but this will get compiled:
但这将被编译:
println(0::1::2::3::4::5::Nil)


