Skip to content

Instantly share code, notes, and snippets.

@MaksimDmitriev
Created November 1, 2023 11:31
Show Gist options
  • Save MaksimDmitriev/21db2de414a6b3531c16562d4ec6876b to your computer and use it in GitHub Desktop.
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
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