Skip to content

Instantly share code, notes, and snippets.

@rajendhirandev
Created October 9, 2022 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajendhirandev/36b531f7bd329febef335e79ecddddd1 to your computer and use it in GitHub Desktop.
Save rajendhirandev/36b531f7bd329febef335e79ecddddd1 to your computer and use it in GitHub Desktop.
Just Generic Code Snippet for reference
// If we use out / in <out T> / <in T> restricts for getter and setter. Also, variance can be used on Classes and Interfaces.
abstract class Test<T> {
abstract fun getTest(input: T): T
}
class TestImpAbs : Test<Int>() {
override fun getTest(input: Int): Int {
return 0
}
}
interface TestClass<T> {
fun getTest(): T
}
class TestImp : TestClass<String> {
override fun getTest(): String {
return ""
}
}
// Can't use Variance (out/in) on the function.
fun <T> test(classa: T): T {
return classa
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment