【问题标题】:What does the <% operator mean in Scala generics? [duplicate]Scala 泛型中的 <% 运算符是什么意思? [复制]
【发布时间】:2012-07-15 23:41:42
【问题描述】:

在 specs2 中有一个名为 Around 的方法,documented here,其示例如下:

object http extends Around {
  def around[T <% Result](t: =>T) = openHttpSession("test") {
    t  // execute t inside a http session
  }
}

此代码的来源可以在here找到。

我很好奇

编辑:这是关于这个主题的可靠答案,What are Scala context and view bounds?

【问题讨论】:

  • Google 很难搜索符号,但 PDF 查看器非常好:看看Scala Language Specification(或“SLS”)。这种类型限制的术语是“View Bounds”。
  • 我必须使用这个特定于符号的搜索引擎才能找到它:symbolhound.com

标签: scala generics specs2


【解决方案1】:

这是一个视图绑定。这意味着T 类型必须可转换为Result 类型。有关类型边界的更多信息,我建议您 http://www.cs.uwaterloo.ca/~brecht/courses/702/Possible-Readings/scala/ProgrammingInScala.pdf,从第 61 页开始。

【讨论】:

    猜你喜欢
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 2011-04-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-26
    相关资源
    最近更新 更多