https://kotlinlang.org logo
Title
f

fabianterhorst

07/25/2017, 10:13 AM
Is kotlin generating setters for vars that are private as well, or how can i prevent the compiler from generating setters for var´s
r

rafal

07/25/2017, 10:25 AM
use
@JvmField
to prevent from generating both setters and getter - property will then be plain old java field
f

fabianterhorst

07/25/2017, 10:27 AM
great, thanks for the answer
k

kingsley

07/25/2017, 10:32 AM
For simple
private var
, there are no setters generated
f

fabianterhorst

07/25/2017, 10:33 AM
private var is generating setters
is there an way for auto generated getters but not for setters
k

kingsley

07/25/2017, 10:36 AM
For simple
private var age = 5
for example. It wouldn’t However, if your var is via delegation, then it will generate get/set Also, if you try to access the var from an inner class, then synthetic get/set will be generated
f

fabianterhorst

07/25/2017, 10:37 AM
i just checked the dex, an private var like private var id: String = "" generates an getter and sette
its an simple class without an inner class
k

kingsley

07/25/2017, 10:41 AM
I have this:
class Me {
    private var id: String = ""
}
And Decompiled:
public final class Me {
   private String id = "";
}
f

fabianterhorst

07/25/2017, 10:46 AM
i know where the problem is for me
i have an class . inside the class but i thought with inner class you mean the syntax 'inner class'
k

kingsley

07/25/2017, 10:50 AM
Inner class could be any inner class (with or without
inner
,
object
) accessing the variable
So, the setter being generated should be synthetic. Yes?
f

fabianterhorst

07/25/2017, 11:01 AM
yes
k

kingsley

07/25/2017, 11:01 AM
Cool. There you go
f

fabianterhorst

07/25/2017, 11:01 AM
when using jvm field im not getting the setters generated
i would like something between, an package private field with an getter
k

kingsley

07/25/2017, 11:03 AM
Yea. But you can't use
@JvmField
on a private property. So no need to generate synthetic assessors
Kotlin does not have package private visibility
f

fabianterhorst

07/25/2017, 11:07 AM
maybe i should just use an var and doesnt care about the generated getter
setter*
r

rafal

07/25/2017, 11:13 AM
Can I ask, why are you bothered by getters/setters? Method count?
f

fabianterhorst

07/25/2017, 11:14 AM
yes thats the reason
maybe there are even deleted by proguard in the end
r

rafal

07/25/2017, 11:16 AM
I'm not sure but I remember something about proguard replacing simple setters/getters with direct field access.
And for
private var
compiler shouldn't generate accessors - that's why
@JvmField
has not effect on private property