Stylianos Gakis
06/14/2024, 10:48 AMinterface A
interface B
class Foo : A, B
fun <T> foo(): T
where T : A,
T : B {
return Foo()
}
But on Foo() I get:
Type mismatch.
Required: T
Found: Foo
Would it just be safe here to do return Foo() as T
?
p.s: In my real impl the function body has many branches of course, and all of them match the T description of inheriting from both interfacesephemient
06/14/2024, 10:54 AMclass Bar : A, B
val bar = foo() // foo<Bar>()
Stylianos Gakis
06/14/2024, 11:37 AMephemient
06/14/2024, 11:51 AMStylianos Gakis
06/14/2024, 12:16 PMinterface Destination
Then in some feature, I got a sealed hierarchy of some destinations that look like this
sealed interface FeatureFooDestination {
data object ScreenOne : FeatureFooDestination, Destination
data class ScreenTwo(val x: Int) : FeatureFooDestination, Destination
}
And I can not mark the FeatureFooDestination
as Destination
itself since the you can accidentally try to navigate to that which is just not a thing that is possible (using androidx.navigation with the new type-safe capabilities).
Then I got a function which is returning one possible FeatureFooDestination
, so fun getFeatureFooDestination(): FeatureFooDestination
and I want to pass the result of that to the fun <T : Destination> NavBackStackEntry.navigate(destination: T)
so there's the problem, since I can't pass the FeatureFooDestination
returned to the navigate function, since the interface itself is not a Destination
even though I know that all of the subclasses are that.
What I've done for now is just have the fun getFeatureFooDestination()
return Destination
instead since I am only interested in passing that to the navigate
function afterwards. This does technically does open the room for error where I return another type of Destination from that function, but since all of this is internal to that module I should be fine I think.