Skip to content

Instantly share code, notes, and snippets.

@luangs7
Last active July 17, 2018 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luangs7/cc26412335c949c467f2c7294be66037 to your computer and use it in GitHub Desktop.
Save luangs7/cc26412335c949c467f2c7294be66037 to your computer and use it in GitHub Desktop.
An example of creating a Swift like guard let extension function in Kotlin for EditText viewgroup
inline infix fun EditText.guard(call: () -> Unit): String? {
if (this.text.isNotEmpty()) return text
else {
call()
return null
}
}
//example of usage
val name = field.guard{
println("This field cant be empty!")
return null
}
println($name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment