Skip to content

Instantly share code, notes, and snippets.

@Serchinastico
Created September 11, 2018 13:07
Show Gist options
  • Save Serchinastico/67d0c0682eb801f9f7229427fc6cec55 to your computer and use it in GitHub Desktop.
Save Serchinastico/67d0c0682eb801f9f7229427fc6cec55 to your computer and use it in GitHub Desktop.
Nullability exercise
fun sum1(a: Int?, b: Int?): Int {
b ?: return 0
return if (a != null) {
a + b
} else {
5 + b
}
}
fun sum2(a: Int?, b: Int?): Int {
val first = a ?: 5
val second = b ?: return 0
return first + second
}
fun sum3(a: Int?, b: Int?): Int = when {
b == null -> 0
a == null -> 5 + b
else -> a + b
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment