In the following code, I'm trying to instantiate the trait A with the Decorator
class and add a method p
to it so I can get another object A but with a p
method:
trait A {
def x: Int
}
case class Decorator(a: A) {
def withPrint: A = new A {
val x = 100
def p: Unit = println(x)
}
}
val a = new A {val x = 100}
val d = Decorator(a).withPrint
But when I try to call d.p
it gives error
value p is not a member of A$A54.this.A
Also then I print the declared methods in d
I see p
is private!
d.getClass.getDeclaredMethods.foreach(println)
# public int stringsimilarity.A$A55$A$A55$Decorator$$anon$2.x()
# private void stringsimilarity.A$A55$A$A55$Decorator$$anon$2.p()
Could anyone explain why this happens? Any help will be really appreciated!! Thank you!