Access field from trait in companion object
scala
Solution
Declare `val CONST_VALUE = 10` inside the companion object `A` instead of trait `A`. Also corrected the `apply` method definition in object `B`
trait A {
}
object A {
final val CONST_VALUE = 10
}
class B(someValue: Int, values: Array[Int]) extends A {
//some methods
}
object B {
def apply(someValue: Int) = new B(someValue, Array.ofDim[Int](someValue).flatMap(block => Array.fill[Int](A.CONST_VALUE)(0)))
}
Problem
I have something like the following code (I simplified it): ``` trait A { val CONST_VALUE = 10 } class B(someValue: Int, values: Array[Int]) extends A { //some methods } object B { def apply(someValue: Int) = B(someValue, Array.ofDim[Array[Byte]](someValue).map(block => Array.fill[Byte](A.CONST_VALUE)(0))) } ``` Basically, I declared a constant `CONST_VALUE` in the trait `A`. I am trying to use it in the companion object `B` to instantiate the class `B`. However, I can't access `A.CONST_VALUE` from the companion object `B`.(I'm getting a compilation error). So how could I do this?