What does double colon (or colon-colon) :: mean in Scala?

你。 提交于 2019-12-11 15:56:14

问题


I was having an issue with the sbt build of my Scala project (duplicate entry: META-INF/MANIFEST.MF) and the following lines solved the problem:

assemblyMergeStrategy in assembly := {
  case PathList("META-INF", xs @ _*) =>
    (xs map {_.toLowerCase}) match {
      case ("manifest.mf" :: Nil) | ("index.list" :: Nil) | ("dependencies" :: Nil) => MergeStrategy.discard
      case _ => MergeStrategy.last
    }
}

I am now trying to understand what the double colon means in the above context. I found an answer in Quora, but this did not help.

Moreover, I am a newbie to Scala which does not really help.

Edit:

Seeing the answers, I still don't understand why since we are creating a list the following does not work (duplicate manifest.mf entries are not discarded):

case List("manifest.mf", "index.list", "dependencies") => MergeStrategy.discard

Neither does the following: (error: ')' expected but '=>' found.):

case List("manifest.mf") | List(("index.list") | List("dependencies") => MergeStrategy.discard

Edit 2:

Removing the extra bracket made the following work:

case List("manifest.mf") | List("index.list") | List("dependencies") => MergeStrategy.discard

And for those interested into the topic... 5 ways to create lists in Scala.


回答1:


In general, "manifest.mf" :: Nil is the same with `List("manifest.mf"). It is just a way to make or pattern match a list.

There are good resources for how pattern matching (also search unapply) work in Scala, mainly used in case statements.

Here it says: If that list has one element, and that is one of (manifest.mf,index.list, dependencies) then pick MergeStrategy.discard




回答2:


It's a name of a class (yes, :: is a valid name of a class). It's resolved as Nil.::(arg) since every method, that's ending with colon is right associative, which in turn resolves to ::(arg, Nil). This constructs a list with given parameters (prepends the element to existing list or creates one if invoked on Nil).



来源:https://stackoverflow.com/questions/55849682/what-does-double-colon-or-colon-colon-mean-in-scala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!