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
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
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 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 answerToEverything() = 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 View.makeInvisible() { | |
this.visibility = View.INVISIBLE | |
} | |
val button: Button = //... | |
button.makeInvisible() |
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 handleStateChange(newState: UIState) { | |
when (newState) { | |
is LoadingState -> println("Loading ...") | |
is DataState -> println("Got data: ${newState.data}") | |
is ErrorState -> println("Error: ${newState.error.message}") | |
} | |
} |
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
sealed class UIState | |
object LoadingState : UIState() // Data is being loaded | |
class DataState(val data: Data) : UIState() // Data loaded successfully | |
class ErrorState(val error: Throwable) : UIState() // In case of failure |
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
// Example of data class (saved more than 10 lines in comparison to Java) | |
data class User(val name: String, val surname: String, val age: Int) | |
// Example of custom getters and setters on a property | |
class MrUser { | |
var name: String = "" | |
get() { | |
return "Mr. $field" // Get name with "Mr." prefix | |
} | |
set(newValue) { |
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
class InitOrderDemo(name: String) { | |
val firstProperty = "First property: $name".also(::println) | |
init { | |
println("First initializer block that prints ${name}") | |
} | |
val secondProperty = "Second property: ${name.length}".also(::println) | |
init { |