Skip to content

Instantly share code, notes, and snippets.

@Allan-Gong
Created January 9, 2018 22:23
Show Gist options
  • Save Allan-Gong/de32be7cfa3e541d02f33b776b488dc9 to your computer and use it in GitHub Desktop.
Save Allan-Gong/de32be7cfa3e541d02f33b776b488dc9 to your computer and use it in GitHub Desktop.
apply V.S with in Kotlin
// Usage of apply: do something with an object and return it
fun getDeveloper(): Developer {
return Developer().apply {
developerName = "Amit Shekhar"
developerAge = 22
}
}
// Usage of with: transform A -> B
fun getPersonFromDeveloper(developer: Developer): Person {
return with(developer) {
Person(developerName, developerAge)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment