Skip to content

Instantly share code, notes, and snippets.

@Audhil
Created February 27, 2020 10:13
Show Gist options
  • Save Audhil/93a40db33b3e186d3813a1d9e588e5a5 to your computer and use it in GitHub Desktop.
Save Audhil/93a40db33b3e186d3813a1d9e588e5a5 to your computer and use it in GitHub Desktop.
kotlin scope function
fun main() {
val valueIs = SomeObj()
val result = valueIs.apply {
this.firstName = "Mohammed"
this.secondName = "Audhil"
}
println(valueIs) // SomeObj(firstName=Mohammed, secondName=Audhil)
println(result) // SomeObj(firstName=Mohammed, secondName=Audhil)
}
private class SomeObj {
var firstName: String? = null
var secondName: String? = null
override fun toString(): String {
return "SomeObj(firstName=$firstName, secondName=$secondName)"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment