Getting to know some pragmatic programming language features

Issue #270 As you know, in the Pragmatic Programmer, section Your Knowledge Portfolio, it is said that Learn at least one new language every year. Different languages solve the same problems in different ways. By learning several different approaches, you can help broaden your thinking and avoid getting stuck in a rut. Additionally, learning many languages is far easier now, thanks to the wealth of freely available software on the Internet...

May 23, 2019 · 8 min · 1570 words · Khoa

Using camelCase for abbreviations

Issue #147 Each language and platform has its own coding style guide. This goes true when it comes to abbreviations. I’ve had some debates about whether to use JSON or Json, URL or Url, HTTP or Http. I personally prefer camelCase, so I’m very happy to see that Kotlin is on my side. See Kotlin Style guide, I think this guide should be applied in other languages, such as Swift 😛...

January 26, 2018 · 2 min · 311 words · Khoa

How to use Function Literals with Receiver in Kotlin

Issue #139 From https://kotlinlang.org/docs/reference/lambdas.html class HTML { fun body() { ... } } fun html(init: HTML.() -> Unit): HTML { val html = HTML() // create the receiver object html.init() // pass the receiver object to the lambda return html } html { // lambda with receiver begins here body() // calling a method on the receiver object } From https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/src/kotlin/util/Standard.kt @kotlin.internal.InlineOnly public inline fun <T> T.apply(block: T.() -> Unit): T { contract { callsInPlace(block, InvocationKind....

January 5, 2018 · 1 min · 152 words · Khoa

Learning from Open Source Making Deferred in Kotlin

Issue #135 From https://github.com/JakeWharton/retrofit2-kotlin-coroutines-adapter/blob/master/src/main/java/com/jakewharton/retrofit2/adapter/kotlin/coroutines/experimental/CoroutineCallAdapterFactory.kt#L86 override fun adapt(call: Call<T>): Deferred<T> { val deferred = CompletableDeferred<T>() deferred.invokeOnCompletion { if (deferred.isCancelled) { call.cancel() } } call.enqueue(object : Callback<T> { override fun onFailure(call: Call<T>, t: Throwable) { deferred.completeExceptionally(t) } override fun onResponse(call: Call<T>, response: Response<T>) { if (response.isSuccessful) { deferred.complete(response.body()!!) } else { deferred.completeExceptionally(HttpException(response)) } } }) return deferred }

January 3, 2018 · 1 min · 56 words · Khoa

Understanding suspend function in Kotlin Coroutine in Android

Issue #123 Getting to know Coroutine From https://kotlinlang.org/docs/reference/coroutines.html To continue the analogy, await() can be a suspending function (hence also callable from within an async {} block) that suspends a coroutine until some computation is done and returns its result: From https://kotlinlang.org/docs/tutorials/coroutines-basic-jvm.html We are using the delay() function that’s like Thread.sleep(), but better: it doesn’t block a thread, but only suspends the coroutine itself. The thread is returned to the pool while the coroutine is waiting, and when the waiting is done, the coroutine resumes on a free thread in the pool....

December 18, 2017 · 5 min · 1042 words · Khoa

Understanding let, apply, with, run in Kotlin

Issue #114 Picture worths thousand words. Code worths thousand pictures. I don’t understand much until I take a look at Standard.kt in Kotlin standard library. /** * Calls the specified function [block] with `this` value as its receiver and returns `this` value. */ @kotlin.internal.InlineOnly public inline fun <T> T.apply(block: T.() -> Unit): T { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } block() return this } /** * Calls the specified function [block] with `this` value as its argument and returns its result....

December 5, 2017 · 1 min · 203 words · Khoa

Communication between Fragment and Activity

Issue #108 There’s always need for communication, right 😉 Suppose we have OnboardingActivity that has several OnboardingFragment. Each Fragment has a startButton telling that the onboarding flow has finished, and only the last Fragment shows this button. Here are several ways you can do that 1. EventBus 🙄 Nearly all articles I found propose this https://github.com/greenrobot/EventBus, but I personally don’t like this idea because components are loosely coupled, every component and broadcast can listen to event from a singleton, which makes it very hard to reason when the project scales...

November 21, 2017 · 3 min · 542 words · Khoa