@ParseClassName("Book")
class Book : ParseObject() {
// getString() and put() are methods that come from ParseObject
var name: String
get() = getString("name")
set(value) = put("name", value)
var author: String
get() = getString("author")
set(value) = put("author", value)
}
Fetching these values would look similar to using property access syntax with other models:
val book = api.getBook()
textAuthor.text = book.author
Now if your model needed to change from Parse to some other data source, your code would potentially only need to be changed in one place.
3. Lambdas
Lambdas reduce the overall lines of code in a souce and allow for funcational programming . While lambdas are currently possible with Android, Kotlin take them a step further byu ensuring you don’t have to deal with RetroLambda or chaning the way your build is configured
For example, an on-click listener would look like:
button.setOnClickListener { view ->
startDetailActivity()
}
It even works with return values:
toolbar.setOnLongClickListener {
showContextMenu()
true
}
4. Data Classes
Data classes simplify classes, adding equals(), hashcode(), copy(), toString() methods automatically.
data class User(val name: String, val age: Int)
That’s it. Nothing else is needed to make this class work. If you are using data classes with something like Gson or another JSON parsing library , you can create the default constructor with default values like so:
// Example with Gson's @SerializedName annotation
data class User(
@SerializedName("name") val name: String = "",
@SerializedName("age") val age: Int = 0
)
 5. Global Constants
Kotlin allows you to define constants that span across an entire app in one place . By using ” const val ”
package com.savvyapps.example
import android.support.annotation.StringDef
// Note that this is not a class, or an object
const val PRESENTATION_MODE_PRESENTING = "presenting"
const val PRESENTATION_MODE_EDITING = "editing"
These can be used as constants anywhere in your project:
import com.savvyapps.example.PRESENTATION_MODE_EDITING
val currentPresentationMode = PRESENTATION_MODE_EDITING