上次上海scala聚会回来,把《快学scala》借给王福强看了几天,他写过一篇”scala for the impatient琐碎记录”,其中 多Trait与继承的解读
class A extends B with C with D with E
应做类似如下形式解读:
class A extends (B with C with D with E)
这正是《scala for the impatient》这本书上的内容,我下面的理解也基本源于这本书。
T1 with T2 with T3 …
这种形式的类型称为复合类型(compound type)或者也叫交集类型(intersection type)。
跟结构类型类似,可以在一个方法里声明类型参数时使用复合类型:
scala> trait X1; trait X2;
scala> def test(x: X1 with X2) = {println("ok")}
test: (x: X1 with X2)Unit
scala> test(new X1 with X2)
ok
scala> object A extends X1 with X2
scala> test(A)
ok
也可以通过 type 声明:
scala> type X = X1 with X2
defined type alias X
scala> def test(x:X) = println("OK")
test: (x: X)Unit
scala> class A extends X1 with X2
scala> val a = new A
scala> test(a)
OK
在上一篇介绍结构类型时也提到过复合类型中也可包含结构类型:
scala> type X = X1 with X2 { def close():Unit }
defined type alias X
scala2.10.3 不能用 是有特别原因
object TypeSystemCompundType extends App {
//class A extends (B with C with D with E)
trait X1; trait X2;
val o = new X1 with X2
type X = X1 with X2
// val o2 = new X//出错
// object T extends X//出错 class type required but hongjiang.info.TypeSystemCompundType.X1 with hongjiang.info.TypeSystemCompundType.X2 found
// trait XX extends X//出错
}
没明白你的意思?
你好,我最近看akka 的文档中微内核部分,发现他有一个akka-sbt-plugin 插件,用来创建一个dist包,但是我google好久都不知道在哪个仓库上去下载相应的akka-sbt-plugin,很多版本都试过,请问是这个插件被废弃了吗还是重新更改了名字?
参考http://stackoverflow.com/a/21451982/2073130,这个akka-sbt-plugin已经废弃了,现在使用SBT Native Packager。
好的,谢谢~
哎,看你在slideshare上的ppt还蛮有条理的,这里的博客倒是有顺序上的问题,继承怎么会放到这么晚呢?只是记录学习的过程的话还情有可原,但是就只能target已经熟悉scala的读者,布道功能没有了。
怎么解释这个情况?
scala> trait T1
defined trait T1
scala> trait T2
defined trait T2
scala> type T = T1 with T2
defined type alias T
scala> class A extends T
:13: error: class type required but T1 with T2 found
class A extends T
^
scala>
这个我解释不了,也可能是编译器的bug,你到scala user 邮件列表里问问吧
http://stackoverflow.com/questions/12943106/good-style-type-aliases-vs-subclassing-empty-classes-traits