Hackernoon logoKotlin’s development pot holes found in 3 months by@elye.project

Kotlin’s development pot holes found in 3 months

As mentioned earlier, it has been 3 months developing Kotlin again after Google endorsed it in Google IO 17. Among the great things enjoying, there’s also various limitations found. Just to share a few here.

Smart-casting limitation, not checking beyond the function

I have a function as below

private fun String?.isGood(): Boolean {
   return this != null && !this.isNullOrEmpty() && this.check()

You’ll notice there’s a redundant check this != null since we already have !this.isNullOrEmpty(). It is required, the compiler will assume it could still be null at this.check(). Smart-casting to can’t check internal to !this.isNullOrEmpty().

Companion Object is not inheritable in Kotlin, but accessible in Java

open class KotlinBase {
    companion object { const val TAG = “testing” }
class KotlinChild : KotlinBase()

When I try access the TAG as below

class KotlinOther {
    val test1 = KotlinBase.TAG // This is okay
    val test2 = KotlinChild.TAG // Compile/Syntax error

However if you access through java, they are okay.

public class JavaOther {
    String test1 = KotlinBase.TAG; // This is okay
    String test2 = KotlinChild.TAG; // This is okay

Custom View constructor overload

It is cool we have @JvmOverloads to help shrinking the 4 Custom View constructor to one constructor.

class MyView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, 
    defStyle: Int = 0, defStyleRes: Int = 0
) : LinearLayout(context, attrs, defStyle, defStyleRes)

However, this will crash if you support Android SDK 19 and earlier. You’ll need to define it as below instead, separating out defStyleRes which is only available in SDK 21 and later.

class MyView : LinearLayout {
    constructor(context: Context, attrs: AttributeSet? = null,
        defStyleAttr: Int = 0)
    : super(context, attrs, defStyleAttr)
    constructor(context: Context, attrs: AttributeSet?, 
        defStyleAttr: Int, defStyleRes: Int)
    : super(context, attrs, defStyleAttr, defStyleRes)

Warning on EventBus unused parameter

In using EventBus, the parameter are used as a flag to know where it would land. If there’s nothing we need to pass along, the parameter is not used as below.

fun onEvent(event: EventTypes.MyEvent) {   
    trigger(EVENT_TAG, MY_EVENT)

Kotlin compipler will complain as below.

MyKotlinClass.kt: (11, 33): Parameter ‘event’ is never used

There’s a good intention of checking this thought I have to admit. So I just use @Suppress(“UNUSED_PARAMETER”) to suppress it :P

Gson deserialized result could make non-null variable null.

Null Safety is cool. But the example below would break it. A null variable could be printed, and still works without error, until you try to access it.

fun forceNull() {
    val nullBody = “””{ “nonNull” : null}”””
    val nullNonNull = Gson().fromJson(nullBody, MyData::class.java)
data class MyData (val nonNullString :String)

A post Gson deserialization check is needed to protect yourselves.

Coverage Support broken in Android Gradle Plugin 3.0.0-beta2

I think among all, this would be the biggest hole. Coverage doesn’t work on latest Android Studio :(

Android Studio 3.0 gradle 3.0.0-beta2, breaks Kotlin Unit Test Coverage?

Well, these all are Pot Holes. They are still not Sinkhole. So we could still walk the journey to ship the product. Nonetheless, hopefully it would be patched soon.

If you like my post and want to get future update, follow me at medium~Twitter:elye; Facebook:elye


The Noonification banner

Subscribe to get your daily round-up of top tech stories!