Jérémy CROS
07/21/2021, 12:30 PMclass Foo
class Bar(
private val foo: Foo
) {
override fun equals(other: Any?): Boolean {
if(other !is Bar) return false
return this.foo == other.foo // it works?
}
override fun hashCode(): Int {
return foo.hashCode()
}
}
fun test() {
val foo = Foo()
val bar = Bar(foo)
val test = bar.foo // private, cannot access
}
Could not find any documentation on this behavior.
I mean, seemed convenient but it just felt... weird that it worked on the equals override 🙂Cicero
07/21/2021, 12:34 PMmcpiroman
07/21/2021, 12:39 PMJérémy CROS
07/21/2021, 12:56 PMmcpiroman
07/21/2021, 1:04 PMFoo
you can access private members of any object of type `Foo`which just happens to be most used with the `this`parameter. You may even access these in companion objects, other objects, nested classes etc as long as they are inside the `Foo`class.mcpiroman
07/21/2021, 1:05 PMclass Foo {
private var bar = 1
companion object {
fun baz(foo: Foo) {
foo.bar++
}
}
}
Jérémy CROS
07/21/2021, 1:09 PMLandry Norris
07/21/2021, 7:55 PM