Created
September 11, 2018 13:07
-
-
Save Serchinastico/67d0c0682eb801f9f7229427fc6cec55 to your computer and use it in GitHub Desktop.
Nullability exercise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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