Scala 中链式包子句背后的动机是什么? [英] What's the motive behind Chained Package clauses in Scala?

查看:15
本文介绍了Scala 中链式包子句背后的动机是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Scala 2.8 中引入了链式包子句,正如 Martin Odersky 在 Scala 网站.我不太明白这背后的直觉.

Chained package clause were introduced in Scala 2.8, as described by Martin Odersky on the Scala site. I don't quite get the intuition behind this.

以下是 Scala 书中嵌套包的示例:

Following was the example in the Scala book for the nested packages:

package bobsrockets {
    package navigation {
        // In package bobsrockets.navigation
        class Navigator
        package tests {
            // In package bobsrockets.navigation.tests
            class NavigatorSuite
        }
    }
}

嵌套包的这个用例是有意义的,因为我们可以在同一个文件中使用多个嵌套包,但是新语法实现了与以前相同的功能,但没有括号.在简洁的 Scala 代码之间分离出包会不会很困难?

This use case of nested packages made sense because we could use multiple nested packages in the same file, however the new syntax achieves the same thing as before but without the brackets. Won't it be difficult to separate out the package in between the succinct Scala code?

package bobsrockets
package navigation
// In package bobsrockets.navigation
class Navigator
package tests
// In package bobsrockets.navigation.tests
class NavigatorSuite

如果我理解错误或误解了这个概念,请告诉我.

Please let me know if I'm getting it the wrong way or if I misunderstand the concept.

推荐答案

可以按照您的示例显示的方式使用不带括号的语法,但我在现实生活"中从未见过这种情况.我认为几乎总是新功能只是用于获取范围内的父包:

You can use the syntax without brackets in the way your example shows, but I never saw this in "real life". I think almost always the new feature is simply used to get parent packages in scope:

package bobrockets.navigation
package tests

//now the content of bobrockets.navigation is in scope

这和写的基本一样

package bobrockets.navigation.test
import bobrockets.navigation._

但是,第一个版本遵循 DRY 原则.例如.如果您将包 bobrockets 重命名为 robertsrockets,您可能会忘记在第二个版本中更改导入(这可能指向一些旧"代码),这在第一个版本中是不可能的.从某种意义上说,这(连同像 private[bobsrockets.navigation] 这样的修饰符的可能性)允许使用非常轻量级的语法将包组用作模块"或超级包".

However, the first version follows the DRY principle. E.g. if you rename the package bobrockets to robertsrockets, you could forget to change the import in the second version (which might point to some "old" code), which is impossible in the first version. In a sense, this (together with the possibility to have modifiers like private[bobsrockets.navigation]) allows to use package groups as "modules" or "superpackages" with a very lightweight syntax.

这是我所知道的主要用法,但 Scala 经常显示出令人惊讶的协同效应,并且模糊了有趣的界限(例如,对象、包和包对象之间、val 和对象之间、defs 和函数之间等)方法.所以未来会展示这个功能是否还有其他有用的应用.

This is the main usage I'm aware of, but Scala shows often surprising synergy effects, and is blurring the lines (e.g. between objects, packages and package objects, between vals and objects, between defs and functions etc) in interesting ways. So the future will show if this feature has other useful applications.

[更新] 以下是 Martin Odersky 本人关于此主题的新文章:http://www.artima.com/scalazine/articles/chained_pa​​ckage_clauses_in_scala.html

[Update] Here is a new article about this topic by Martin Odersky himself: http://www.artima.com/scalazine/articles/chained_package_clauses_in_scala.html

这篇关于Scala 中链式包子句背后的动机是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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