Skip to content

Instantly share code, notes, and snippets.

@Audhil
Created February 27, 2020 10:09
Show Gist options
  • Save Audhil/00cba526dcd648a16b5fe1ca84936c85 to your computer and use it in GitHub Desktop.
Save Audhil/00cba526dcd648a16b5fe1ca84936c85 to your computer and use it in GitHub Desktop.
kotlin scope function
fun main() {
val valueIs = SomeObj()
val result = valueIs.also {
it.firstName = "Mohammed"
it.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