在Scala中,双冒号(或冒号)::是什么意思? [英] What does double colon (or colon-colon) :: mean in Scala?

查看:950
本文介绍了在Scala中,双冒号(或冒号)::是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Scala项目的 sbt 版本中遇到问题(重复项: META-INF / MANIFEST.MF )和以下几行代码解决了该问题:

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
    }
}

我现在试图理解双冒号在什么意思以上情况。我在 Quora中找到了答案,但这无济于事。

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.

此外,我是Scala的新手,实际上并没有帮助。

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

编辑:

看到答案,我仍然不明白为什么原因,因为我们正在创建列表,因此以下内容不起作用(重复 manifest.mf 项不会被丢弃):

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

以下都不是:(error:')'预期,但'=> ):

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

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

编辑2:

去掉多余的括号即可完成以下工作:

Removing the extra bracket made the following work:

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

对于那些对此主题感兴趣的人... 在Scala中创建列表的5种方法

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

推荐答案

通常, manifest.mf :: Nil 与`List( manifest.mf)相同。
只是一种制作或

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

对于Scala中的模式匹配(也可以搜索 unapply )如何工作,有很好的资源,主要用于 case 语句。

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

此处t表示:如果该列表具有一个元素,并且是( manifest.mf index.list 之一, 依赖性),然后选择 MergeStrategy.discard

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

这篇关于在Scala中,双冒号(或冒号)::是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆