If I do a guard first, then compiler is not enforc...
# compiler
u
If I do a guard first, then compiler is not enforcing exhaustiveness on
when
. Is that a bug?
Copy code
override fun onHandleDeeplink(result: DeeplinkResult) {
    val deeplink = result.deeplink
    if (deeplink !is SubscriberRootDeeplink) return

    when (deeplink) {
        is SubscriberRootDeeplink.SmartId -> {
            ...
        }
        ...
    }
}
d
It's an unsupported feature.
u
won't be or should I file it?
m
I believe that's this actually.
d
Marat is right. It works by default since 2.3: https://pl.kotl.in/ZUEa1EecZ
u
but what's supposed to work? should my code not compile?
d
It's hard to judge your case without seeing the
DeeplinkResult
hierarchy.
u
Copy code
fun UserRootDeeplinkHandler.onHandleDeeplink(result: DeeplinkResult) {
    val deeplink = result.deeplink
    if (deeplink !is UserRootDeeplink) return

    when (deeplink) {
        is UserRootDeeplink.SmartId -> ...
        is MultiRootDeeplink.ResolveRemotely -> ...
    }
}


data class DeeplinkResult(val deeplink: Deeplink, val rawDeeplink: Url)

sealed interface Deeplink

sealed interface LoggedOutRootDeeplink : Deeplink {
    data class Login(val code: OAuthCode, val deeplinkToReplay: Url?) : LoggedOutRootDeeplink
}

sealed interface UserRootDeeplink : Deeplink {
    data class SmartId(val code: OAuthCode) : UserRootDeeplink
}

sealed interface OnboardingRootDeeplink : Deeplink

sealed interface MultiRootDeeplink : Deeplink {
    data class ResolveRemotely(
        val urlToResolve: Url
    ) : MultiRootDeeplink, LoggedOutRootDeeplink, UserRootDeeplink, OnboardingRootDeeplink
}
okay I normalized it a bit the idea is to have typed deeplinks based on root they belong to so they won't mix on accident - but certain deeplinks can be applied everywhere, so such types extend all of the root interfaces they need to and then in each handler I have to filter away cases it's not interested in
and if I add
Copy code
data class ResolveRemotely2(
        val urlToResolve: Url
    ) : MultiRootDeeplink, LoggedOutRootDeeplink, UserRootDeeplink, OnboardingRootDeeplink
then I'd expect the
when
in the snippet to blow up due to not being exhaustive, but it does not
d
It's green.
u
right, but add new multiroot deeplink type
if I just extract the
when
into a function, it goes red right away (sorry for different types, it's my domain, in the snippet I generalized it for simplicity)
btw now I'm noticing, if I add
data class SmartId2(val code: OAuthCode) : UserRootDeeplink
then it complains about exhaustiveness so it's the
MultiRootDeeplink
that confuses it
d
There is actually an issue, which was fixed quite recently: KT-87189. The fix will be delivered in 2.5.0.
I checked, your code (correctly) fails to compile using recent dev version of the compiler.
u
okay great I'll track it, thank you!
👌 1