Created
October 9, 2022 10:19
-
-
Save rajendhirandev/36b531f7bd329febef335e79ecddddd1 to your computer and use it in GitHub Desktop.
Just Generic Code Snippet for reference
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
// 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