how is property ambiguity resolved? Cant find any ...
# announcements
m
how is property ambiguity resolved? Cant find any info on that
k
musachy: Can you perhaps give some examples? Generally the "closest" property is used.
m
Hi Karel, sorry about that
so if I have a class with a protected property "x" and I have a child class that also defines "x". The compiler rightly complains about it.
k
No problem, just try not do do it in the future simple smile. It's annoying if people from both channels start helping at once.
m
I cant seem to find any info on how to tell the compiler which one to use
k
Can you post some example code.?
m
hum. I was writing an example and just realized that in pure Kotlin it doesnt happen. As you need to "override" the parent property. It is between base Java class and child Kotlin
k
A mixed example is fine too 😄
m
Java base class:
import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Base { protected Logger logger = LoggerFactory.getLogger("Base"); }
Kotlin child class: import org.slf4j.LoggerFactory class Child: Base() { private val logger = LoggerFactory.getLogger("Child") fun test() { logger.debug("ouch") } }
k
Okay and logger.debug uses the Child one, right?
m
The compiler will complain about " logger.debug("ouch")"
yes
k
Hmm, interesting one.
You can get the
Base
logger like this:
(this as Base).logger.debug("ouch")
m
ah!
didn't think of that
k
But you need the Child one, right?
m
well, I actually need the child one 🙂
k
Tbh I don't think there's actually a way to do this.
But for now you can just pick another variable name 🙂
m
fyi
I did used a different name, but it was a splinter in my mind to find the "right" solution. Thanks for the help!
k
A quick tip for using youtrack: format your code like this:
{code:lang=kotlin} actual code {code}
m
fixed. that helps.