Last active
November 2, 2023 02:40
-
-
Save MaksimDmitriev/e6652ce257f185a2c04d66071e22fde0 to your computer and use it in GitHub Desktop.
Kotlin star projection
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
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