Skip to content

Instantly share code, notes, and snippets.

@MaksimDmitriev
Last active November 2, 2023 02:40
Show Gist options
  • Save MaksimDmitriev/e6652ce257f185a2c04d66071e22fde0 to your computer and use it in GitHub Desktop.
Save MaksimDmitriev/e6652ce257f185a2c04d66071e22fde0 to your computer and use it in GitHub Desktop.
Kotlin star projection
class Foo<out T : Number>(private val data: T) {
fun get2(): T {
return data
}
}
class Bar<T>(private val data: T) {
fun get2(): T {
return data
}
}
class SampleTest {
@Test
fun starProjections() {
val a: Number = 1
val foo = Foo(a)
fooHandler(foo)
val s: String = "abc"
val bar = Bar(s)
barHandler(bar)
}
// <S: Number> is mandatory. Doesn't work with just <S>
private fun <S : Number> fooHandler(foo: Foo<S>) {
println(foo.get2())
}
private fun <S> barHandler(bar: Bar<S>) {
println(bar.get2())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment