Predef.locally做了什么,它与Predef.identity有什么不同

Dav*_*ith 37 scala

通过Scala 2.8 Predef类,我发现有一种"本地"方法.尽管我可以说,它与Predef.identity相同,只是有"@inline"注释.它有什么用,为什么它在Predef中很重要(因此可以在Scala的任何地方使用)?

ret*_*nym 44

这里讨论:http://www.scala-lang.org/node/3594

我们的想法是避免程序员错误将"悬空"本地块与对象/类/特征的模板混淆.

object test {
  object a
  {
    val x = 1
  }

  object b

  { // oops, extra newline disassociates this block with the object b
    val x = 1
  }
}
test.a.x
//test.b.x // doesn't compile
Run Code Online (Sandbox Code Playgroud)

如果程序员真的希望该块独立,locally可以使用:

object test {
  object a
  {
    val x = 1
  }

  object b

  locally {
    val x = 1
  }
}
Run Code Online (Sandbox Code Playgroud)

该线程还建议第一个代码产生弃用警告.这尚未添加.