Skip to content

Instantly share code, notes, and snippets.

@vodamiro
Last active April 11, 2019 13:39
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 vodamiro/c7488a88c7045ae4134b438f2b36a5e8 to your computer and use it in GitHub Desktop.
Save vodamiro/c7488a88c7045ae4134b438f2b36a5e8 to your computer and use it in GitHub Desktop.
Bye Java, hello Kotlin alias What do we love about Kotlin @Synetech
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 {
println("Second initializer block that prints ${name.length}")
}
}
InitOrderDemo("hello")
// 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) {
println("old: '$field', new: '$newValue'") // Print change
field = newValue // Store new value
}
}
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)
interface Greeter {
fun greet() { println("Hi!") }
}
val a: Int? = 10
val b: Int = 10
val sum = a ?: 0 + b
val sum = a ?: (0 + b)
fun View.makeInvisible() {
this.visibility = View.INVISIBLE
}
val button: Button = //...
button.makeInvisible()
data class User(val name)
interface UserDataSource {
fun getAllUsers(
onSuccess: (users: List<User>) -> Unit,
onError: (err: Throwable) -> Unit
)
}
// class UpdateDataSourceImpl: UserDataSource { .... }
UpdateDataSourceImpl().getAllUsers(
onSuccess = { users -> print(users) }, // here you consume success state
onError = { err -> print(err.message) } // here you consume error state
)
val answer: Int by lazy {
computeAnswer()
}
fun computeAnswer() = 42
var max = 10
for (i in 1..max) {
// ...
}
int max = 10;
for (int i = 1 ; i <= max ; i++) {
// ...
}
var max = 10
val range = IntRange(1, max)
for (i in range) {
// ...
}
private const val DURATION_IN_MS = 3000L
val fadeInAnimation = AlphaAnimation(0f, 1f).apply {
duration = DURATION_IN_MS
interpolator = AccelerateDecelerateInterpolator()
fillAfter = true
}
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
fun handleStateChange(newState: UIState) {
when (newState) {
is LoadingState -> println("Loading ...")
is DataState -> println("Got data: ${newState.data}")
is ErrorState -> println("Error: ${newState.error.message}")
}
}
fun answerToEverything() = 42
fun answerToEverything(): Int {
return 42
}
fun parseColor(color: String) = when (color.toLowerCase()) {
"red" -> "#f00"
"green" -> "#0f0"
"blue" -> "#00f"
else -> "#fff"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment