https://kotlinlang.org logo
#getting-started
Title
# getting-started
m

Michael de Kaste

02/24/2022, 9:52 AM
Copy code
fun main{
    val scope = getScope()
    when(scope){
        // scope not smartcast and thus error
        is DeeperScope.Impl1, is DeeperScope.Impl2 -> scope.someVar = "2" 
    }
}

interface Scope
sealed interface DeeperScope : Scope{
    var someVar: String
    data class Impl1(override var someVar: String) : DeeperScope
    data class Impl2(override var someVar: String) : DeeperScope
}
fun getScope() : Scope = DeeperScope.Impl2("2")
Why can't scope be smartcast to DeeperScope in the when? Both implementations have their common parent as DeeperScope
e
m

Michael de Kaste

02/24/2022, 1:15 PM
wow a 10 year old ticket for something that seems quite trivial
d

dmitriy.novozhilov

02/24/2022, 1:50 PM
Such smartcasts will be avaliable in K2 compiler
👌 4
9 Views