1
下面是一些代碼,我蒸下來,盡我所能:斯卡拉 - 蛋糕圖案+類型類+實現需要構造函數參數
trait CakeLayer[A] extends {
// typeclass hack to make it play nice with traits
implicit def requireTypeclass: MyTypeclass[A]
val typeclassInVal = requireTypeclass
/* other stuff */
}
class FooImpl
object FooImpl {
implicit object FooImplIsTypeclass extends MyTypeclass[FooImpl]
}
// This works perfectly
class Foo extends CakeLayer[FooImpl] {
override def requireTypeclass = implicitly
}
// This gives me NullPointerException
// Occurs in a function of my "Bar" that contains an anonymous function
// which uses typeclassInVal. it is the first access to that val
// probably due to the parameter in the typeclass constructor?
class BarImpl(x: Int)
object BarImpl {
class BarImplIsTypeclass(x: Int) extends MyTypeclass[BarImpl]
}
class Bar(x: Int) extends CakeLayer[BarImpl] {
val typeclass = new BarImpl.BarImplIsTypeclass(x)
override def requireTypeclass = typeclass
}
這樣做!絕對需要學習更多關於變量在課堂建設中如何初始化的知識。謝謝! – adelbertc
lazy val是潛在的僵局http://www.youtube.com/watch?feature=player_embedded&v=yLbdw06tKPQ –