Created
November 1, 2023 11:31
-
-
Save MaksimDmitriev/21db2de414a6b3531c16562d4ec6876b to your computer and use it in GitHub Desktop.
Kotlin out must be present in the implementation class to make the type covariant
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Source<out T> { | |
fun nextT(): T | |
} | |
class SourceImpl<out T>(private val param: T) : Source<T> { | |
override fun nextT(): T { | |
return param | |
} | |
} | |
class SampleTest { | |
@Test | |
fun foo() { | |
val s: SourceImpl<Int> = SourceImpl(1) | |
// without "out" in SourceImpl, this is not allowed. | |
val s2: SourceImpl<Number> = s | |
println(s2.nextT()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment