在scala中有没有办法使用中间的早期初始化器?
以下是我正在尝试编译的内容:
trait A { val valueA: Int = 0 }
trait B {
val valueB: Int
println(valueB)
}
class C extends A with { val valueB = valueA } with B
编辑:回复Luis的问题
使用scalatest,fixture可以用特征构造函数来组织。我想对一个子subfixture进行参数化,并使用superfixture中的一个字段进行早期初始化。
下面是另一个与实际最具伸缩性的案例更相关的示例:
class Test extends FreeSpec {
trait CommonFixture {
val commonCaseValue: Int = 1
}
abstract trait SpecialCaseFixture {
val specialCaseValue: Int
}
"special case test #1" in new CommonCaseFixture with { val specialCaseValue = commonCaseValue } with SpecialCaseFixture {
// all fixtures fields are accessible here
}
}
转载请注明出处:http://www.hqqscc.com/article/20230526/1483448.html