这是我尽可能多地提炼出来的一些代码:
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
}