The problem is when I try to get the reference to testVar
variable in Kotlin, ambiguity occurs if I have a function with the same name as the variable. Is there a solution for this?
class TestClassBuilder {
lateinit var mainVar: String
lateinit var testVar: String
fun mainVar(mainVar: String) = apply {
this.mainVar = mainVar
}
fun testVar(testVar: String) = apply {
this.testVar = testVar
}
fun build() {
if (!::testVar.isInitialized) {
testVar = mainVar.substring(0, 5)
}
// ...
}
}
I've tried to rename variable and, of course, it worked, but I'd like to know if there is a better solution.
You can't do anything with this problem, but you don't need it. If you use Kotlin properties it will generate setters for you.
First of all, please check how properties work - link
Then you use plain properties:
And here is how you can use setters from Java:
Also you can add visibility modifiers for getters and setters or even custom logic: