Scala类参数化中额外冒号的含义 [英] Meaning of additional colon in Scala class parametrization

查看:319
本文介绍了Scala类参数化中额外冒号的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中, [A:Manifest:WireFormat] 是什么意思?它来自 com.nicta.scoobi.TextInput (在github上可用)。它似乎不是任何常见的类型边界。

What does [A : Manifest : WireFormat] mean in the following code? It's from com.nicta.scoobi.TextInput (available on github). It doesn't seem to be any of the usual type bounds.

  def fromDelimitedTextFile[A : Manifest : WireFormat]
      (path: String, sep: String = "\t")
      (extractFn: PartialFunction[List[String], A])
    : DList[A] = {

    val lines = fromTextFile(path)
    lines.flatMap { line =>
      val fields = line.split(sep).toList
      if (extractFn.isDefinedAt(fields)) List(extractFn(fields)) else Nil
    }
  }

我在哪里可以找到关于这个主题的更多信息?

Where can I find more information about this topic?

推荐答案

这被称为上下文绑定。它们是隐式参数列表的语法糖:

This is called a context bound. They are syntactic sugar for an implicit parameter list:

def meth[A : ContextBound1 : ContextBoundN](a: A)

// ==>

def meth[A](a: A)(implicit evidence: ContextBound1[A], ContextBoundN[A])

如果从1到N有多个上下文边界,它们全部被翻译成相同的参数列表。请参阅此问题,获取关于它们如何工作的更详细说明以及它们有用。

If there are multiple context bounds from 1 to N, they are all translated into the same parameter list. See this question for a more detailed explanation about how they work and for what they are useful.

要找到这样的符号,阅读 StackOverflow Scala教程

To find such symbols it is useful to read the StackOverflow Scala Tutorial.

这篇关于Scala类参数化中额外冒号的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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