简体   繁体   English

此代码片段中紧跟在“new”关键字之后的 cons“::”运算符的含义是什么?

[英]What is the meaning of the cons "::" operator right after the "new" key word in this code snippet?

I'm studying Scala and see in a tutorial the following definition.我正在学习 Scala 并在教程中看到以下定义。 There are 3 " :: ".有 3 个“ :: ”。 My understanding is that the first " :: " is a method in class List , and the third " :: " is a class name.我的理解是第一个“ :: ”是class List一个方法,第三个“ :: ”是一个类名。

But I cannot understand the meaning of the second " :: ".但我无法理解第二个“ :: ”的含义。

sealed abstract class List {
  def ::(head: Int): List = new ::(head, this)
}
case object Nil extends List
case class ::(head: Int, tail: List) extends List

new ClassName ( constructorArguments ) creates a new instance of the class ClassName , passing the constructorArguments as constructor arguments. new ClassName ( constructorArguments )创建类ClassName的新实例,将constructorArguments作为构造函数参数传递。

In your example, :: is the class-name, and head and this are the constructor arguments.在您的示例中, ::是类名, headthis是构造函数参数。

(See https://docs.scala-lang.org/tour/classes.html for more information about constructors.) (有关构造函数的更多信息,请参阅https://docs.scala-lang.org/tour/classes.html 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM