如何解决 2.8.0 中的 Scala 源不兼容问题?
有一些源代码与 Scala 2.8.0 不兼容的情况。例如,创建匿名 Seq
曾经需要定义抽象 def elements : Iterator[A]
,现在称为 def iterator : Iterator[A]
代码>.
对我来说,“强力”解决方案是创建两个与不同主要 scala 版本保持一致的分支。
是否有通用技术可以使这样的代码在两个系统下编译?
// Note: this code resembles techniques used by xml.NodeSeq
trait FooSeq extends Seq[ Foo ] {
def internal : Seq[ Foo ]
def elements = internal.elements
def iterator = internal.iterator // Only compiles in 2.8
// need to remove for 2.7.X
}
There are a few cases of source incompatibilities with Scala 2.8.0. For example, creating an anonymous Seq
once required defining the abstract def elements : Iterator[A]
, which is now called def iterator : Iterator[A]
.
To me, a "brute force" solution is to create two branches that align to the different major scala versions.
Are there general techniques so that code like this will compile under both systems?
// Note: this code resembles techniques used by xml.NodeSeq
trait FooSeq extends Seq[ Foo ] {
def internal : Seq[ Foo ]
def elements = internal.elements
def iterator = internal.iterator // Only compiles in 2.8
// need to remove for 2.7.X
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在某些情况下,用法完全不同,您必须进行更改。但在几乎所有情况下(例如上面的 elements 代码),2.7 样式只是在 2.8 中被弃用,而不是完全消失。如果您可以让 2.8 用户留下弃用警告(编辑:如果他们编译您的代码,否则您自己就会收到警告),只需根据旧功能实现新功能即可:
否则,我会推荐您的功能调用暴力解决方案。使用足够聪明的 VCS,这样您实际上就不必编写两次代码(Git、Bazaar、Mercurial)和分支。
There are a few cases where usage is simply different and you must change. But in almost all cases--such as the elements code above--the 2.7 style is simply deprecated in 2.8, not gone altogether. If you're okay leaving your 2.8 users with deprecation warnings (edit: if they compile your code, otherwise you'll just have the warnings yourself), just implement the new features in terms of the old:
Otherwise, I would recommend what you call the brute force solution. Use a sufficiently clever VCS so that you don't actually have to write much code twice (Git, Bazaar, Mercurial) and branch.