Search code examples
kotlinarrow-kt

Smart cast an Arrow-kt Option


I am trying to smart cast an Option from any Any variable so that I can determine if the Option is empty however the IDE is indicating that Option<*> could not be smart cast because it is declared in a different module.

fun hasEmptyValue(column: Pair<String, Any>): Boolean = when {
    column.second is Option<*> -> column.second.isEmpty()  
    else -> false
}

Solution

  • David Rawson shows how to fix it, but doesn't explain why your code doesn't work.

    The reason is that column.second could in principle return different values for two calls; even though Pair#second is a val, it could have a custom getter method.

    If Pair was in the same module, the compiler could check this, but for other modules it doesn't.