This file contains 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 answerToEverything(): Int { | |
return 42 | |
} |
This file contains 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 parseColor(color: String) = when (color.toLowerCase()) { | |
"red" -> "#f00" | |
"green" -> "#0f0" | |
"blue" -> "#00f" | |
else -> "#fff" | |
} |
This file contains 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 logException(e: Throwable, level = Log.ERROR) { | |
// implementation | |
} | |
// Then you can call the method without providing the second parameter | |
logException(IllegalStateException("Test exception")) | |
logException(IllegalStateException("Test exception"), Log.WARN) |
This file contains 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
interface Greeter { | |
fun greet() { println("Hi!") } | |
} |
This file contains 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
private const val DURATION_IN_MS = 3000L | |
val fadeInAnimation = AlphaAnimation(0f, 1f).apply { | |
duration = DURATION_IN_MS | |
interpolator = AccelerateDecelerateInterpolator() | |
fillAfter = true | |
} |
This file contains 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
val answer: Int by lazy { | |
computeAnswer() | |
} | |
fun computeAnswer() = 42 |
This file contains 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
data class User(val name) | |
interface UserDataSource { | |
fun getAllUsers( | |
onSuccess: (users: List<User>) -> Unit, | |
onError: (err: Throwable) -> Unit | |
) | |
} | |
// class UpdateDataSourceImpl: UserDataSource { .... } |
This file contains 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
var max = 10 | |
for (i in 1..max) { | |
// ... | |
} |
This file contains 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
var max = 10 | |
val range = IntRange(1, max) | |
for (i in range) { | |
// ... | |
} |
This file contains 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
val a: Int? = 10 | |
val b: Int = 10 | |
val sum = a ?: 0 + b |