Scala 中的链式包条款背后的动机是什么?

发布于 2024-09-18 06:31:15 字数 676 浏览 4 评论 0原文

链接包子句是在 Scala 2.8 中引入的,如 Martin Odersky 在 Scala

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

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

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

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

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

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.

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
        }
    }
}

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白馒头 2024-09-25 06:31:15

可以按照示例所示的方式使用不带括号的语法,但我从未在“现实生活”中看到过这种情况。我认为新功能几乎总是简单地用于获取范围内的父包:

package bobrockets.navigation
package tests

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

这与编写However 基本相同

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

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

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

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

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

This is basically the same as writing

package bobrockets.navigation.test
import bobrockets.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.

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.

[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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文