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

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

问题描述

链式包子句是在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中的Chained Package子句背后的动机是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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