Last active
April 11, 2019 13:39
-
-
Save vodamiro/c7488a88c7045ae4134b438f2b36a5e8 to your computer and use it in GitHub Desktop.
Bye Java, hello Kotlin alias What do we love about Kotlin @Synetech
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 { | |
println("Second initializer block that prints ${name.length}") | |
} | |
} | |
InitOrderDemo("hello") |
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) { | |
println("old: '$field', new: '$newValue'") // Print change | |
field = newValue // Store new value | |
} | |
} |
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
val a: Int? = 10 | |
val b: Int = 10 | |
val sum = a ?: 0 + b |
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 sum = a ?: (0 + b) |
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
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 | |
) |
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
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
int max = 10; | |
for (int i = 1 ; i <= max ; i++) { | |
// ... | |
} |
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
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
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
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
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 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" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment