Kotlin’s pills #1 properties introduction
Why is kotlin awesome?
Because unlike java it doesn’t sucks.
For example kotlin have properties:
var test = "hello"
get(){
return field + " world"
}
set(value){
field = value
}
Does this look like the classical Java shit with getters and setters?
Yes it does, but…
You can use it like this
test = "what a shitty"
println(test)
OUTPUT:
what a shitty world
This is the most awesome thing ever, as it makes everything more simple, no more:
dog.getOwner().getDad().setName("Patrick")
This just becomes:
dog.owner.dad.name = "Patrick"
That’s way more synthetic and clear.
Plus it keeps its interoperability with Java since on compile-time it get translated to a private field with public getter and setter.
Another super nice thing is that kotlin automatically turn Java getters and setters into Kotlin properties.
So from Kotlin you can reference a Java class and if it has the correct naming convention you can use its properties without the need to call getters and setters.
Protip:
If the get or the set are single lines you can make it shorter by writing it like this:
var test = "hello"
get() = field + " world"
set(value){ field = value }