https://kotlinlang.org logo
#compose
Title
# compose
m

Madhava

07/23/2020, 2:17 AM
@Zsolt https://github.com/zsoltk/compose-router/pull/35 Any chance you can explain how I can fix the unchecked cast warnings for my PR on compose-router?
l

Leland Richardson [G]

07/23/2020, 4:31 AM
hmm. Just tried what i think is equivalent locally and didn’t have any issues
well, it depends what you’re asking about i guess
Copy code
private val backStackMap: MutableMap<Any, BackStack<*>> =
    mutableMapOf()

class BackStack<T>(initialElement: T)

@Composable
private fun <T> fetchBackStack(key: String, defaultElement: T, override: T?): BackStack<T> {
    // line A
    val existing = backStackMap[key] as BackStack<T>?
    return when {
        // line B
        override != null -> BackStack(override)
        existing != null -> existing
        else -> BackStack(defaultElement)
    }.also {
        backStackMap[key] = it
    }
}
line B should not need any cast
line A does
you don’t have any way of getting around the cast in line A unless you construct the map in such a way that the key itself indicates the element type, but you’d have to change the API of fetchBackStack in order to do that
m

Madhava

07/23/2020, 8:49 AM
When I substitute with the code you have above i still get the same error about BackStack<out T>
I am happy to make any changes you think are needed to get this PR merged as I am using the router in an app which I want to update to dev15. 🙂 https://github.com/zsoltk/compose-router/pull/35
I have updated the PR with your suggestions, still 2 open ones I am unsure what to do about: https://github.com/zsoltk/compose-router/pull/35