Say I have a trait with a property a
:
trait TheTrait {
def a: String
}
I have a class with a property a
too in which I want to instantiate that trait anonymously:
class TheClass {
val a = "abc"
val traitInstance = new TheTrait {
def a = a // I want to assign it to the `a` of TheClass here
// but this way it doesn't work
}
}
How can I achieve this?
either TheClass.this.a
, or give an alias to this
in TheClass
(calling it self
is customary)
class TheClass { self =>
val a = "abc"
val traitInstance = new TheTrait {
def a = self.a
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With