{"id":142,"date":"2026-10-16T03:35:00","date_gmt":"2026-10-16T03:35:00","guid":{"rendered":"https:\/\/moiks.com\/blog\/?p=142"},"modified":"2026-11-06T08:48:35","modified_gmt":"2026-11-06T08:48:35","slug":"kotlin-generics-explained-type-parameters-constraints-variance","status":"publish","type":"post","link":"https:\/\/moiks.com\/blog\/kotlin-generics-explained-type-parameters-constraints-variance\/","title":{"rendered":"Kotlin Generics Explained: Type Parameters, Constraints &#038; Variance"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">When building modern Android applications using <strong>Kotlin and Jetpack Compose<\/strong>, we often need to create reusable components that work with different data types.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, imagine developing an application that handles:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>User information<\/li>\n\n\n\n<li>Product details<\/li>\n\n\n\n<li>Transaction records<\/li>\n\n\n\n<li>API responses<\/li>\n\n\n\n<li>Database results<\/li>\n\n\n\n<li>Pagination data<\/li>\n\n\n\n<li>UI loading states<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Creating separate classes for every data type would lead to unnecessary code duplication.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Kotlin solves this problem using <strong>Generics<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Generics allow developers to create classes, interfaces, and functions that operate on different types while maintaining compile-time type safety.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">They are widely used in Android development, including:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Kotlin Collections<\/li>\n\n\n\n<li>Retrofit API responses<\/li>\n\n\n\n<li>Repository patterns<\/li>\n\n\n\n<li>ViewModel state management<\/li>\n\n\n\n<li>StateFlow<\/li>\n\n\n\n<li>Room database abstractions<\/li>\n\n\n\n<li>Jetpack Compose components<\/li>\n\n\n\n<li>Paging and data transformations<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">In this tutorial, we&#8217;ll explore Kotlin Generics from fundamental concepts to practical Android implementations.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What You&#8217;ll Learn<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">By the end of this tutorial, you&#8217;ll understand:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>What Kotlin Generics are<\/li>\n\n\n\n<li>Why Generics are important<\/li>\n\n\n\n<li>Generic classes<\/li>\n\n\n\n<li>Generic functions<\/li>\n\n\n\n<li>Multiple type parameters<\/li>\n\n\n\n<li>Generic interfaces<\/li>\n\n\n\n<li>Generic constraints<\/li>\n\n\n\n<li>The <code>where<\/code> keyword<\/li>\n\n\n\n<li>Covariance using <code>out<\/code><\/li>\n\n\n\n<li>Contravariance using <code>in<\/code><\/li>\n\n\n\n<li>Invariance<\/li>\n\n\n\n<li>Star projections<\/li>\n\n\n\n<li>Use-site variance<\/li>\n\n\n\n<li>Type erasure<\/li>\n\n\n\n<li>Reified type parameters<\/li>\n\n\n\n<li>Generic API response wrappers<\/li>\n\n\n\n<li>Generic repository patterns<\/li>\n\n\n\n<li>Jetpack Compose generic components<\/li>\n\n\n\n<li>Common mistakes and best practices<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s begin!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. What Are Generics in Kotlin?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Generics allow us to define reusable code using type parameters instead of fixed data types.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Consider the following example.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Without Generics<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class StringBox(\n    val value: String\n)\n\nclass IntBox(\n    val value: Int\n)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Both classes perform the same operation: storing a value.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The only difference is the data type.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Instead of creating separate classes, we can use a generic class.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">With Generics<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Box&lt;T&gt;(\n    val value: T\n)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here, <code>T<\/code> represents a type parameter.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We can use this class with different types.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() {\n\n    val stringBox = Box(\"Hello Kotlin\")\n\n    val intBox = Box(100)\n\n    println(stringBox.value)\n    println(intBox.value)\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello Kotlin\n100<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The same <code>Box&lt;T&gt;<\/code> class works with String, Int, and other types.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The compiler infers the appropriate type from the supplied value.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Generics improve reusability while preserving type safety.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. Why Use Generics in Kotlin?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Generics provide several important benefits.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Code Reusability<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">One generic implementation can work with multiple types.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Type Safety<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The compiler checks whether values match the expected types.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Reduced Duplication<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Developers don&#8217;t need to create separate implementations for every data type.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Better Architecture<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Generics help create reusable repositories, API result wrappers, and data processing components.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Improved Maintainability<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Common functionality can be maintained in one place.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>val names: List&lt;String&gt; = listOf(\n    \"Alex\",\n    \"Emma\",\n    \"John\"\n)\n\nval numbers: List&lt;Int&gt; = listOf(\n    10,\n    20,\n    30\n)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Both collections use the generic <code>List&lt;T&gt;<\/code> interface.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The type argument determines which elements are allowed.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. Kotlin Generic Classes<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A generic class declares one or more type parameters.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class ClassName&lt;T&gt;(\n    val value: T\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example: Generic Container<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Container&lt;T&gt;(\n    private val item: T\n) {\n\n    fun getItem(): T {\n        return item\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Usage<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() {\n\n    val nameContainer = Container(\"Android\")\n\n    val numberContainer = Container(2026)\n\n    println(nameContainer.getItem())\n\n    println(numberContainer.getItem())\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Android\n2026<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The generic class uses <code>T<\/code> as a placeholder for the actual type.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When creating the object, Kotlin determines the type from the constructor argument.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We can also specify it explicitly.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>val container: Container&lt;String&gt; = Container(\"Kotlin\")<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. Kotlin Generic Functions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Generic functions allow us to create reusable functions that work with multiple types.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun &lt;T&gt; functionName(\n    value: T\n): T {\n    return value\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun &lt;T&gt; printValue(value: T) {\n    println(value)\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Usage<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() {\n\n    printValue(\"Hello\")\n\n    printValue(100)\n\n    printValue(true)\n\n    printValue(99.5)\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello\n100\ntrue\n99.5<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The function works with String, Int, Boolean, and Double.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We don&#8217;t need separate functions for each type.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Another Example: Generic Identity Function<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun &lt;T&gt; identity(value: T): T {\n    return value\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Usage:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>val name = identity(\"Alex\")\n\nval age = identity(25)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The returned value preserves the supplied type.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5. Generic Functions with Multiple Parameters<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Generic functions can accept multiple values of the same type.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun &lt;T&gt; createPair(\n    first: T,\n    second: T\n): Pair&lt;T, T&gt; {\n\n    return Pair(first, second)\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Usage<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() {\n\n    val names = createPair(\n        \"Alex\",\n        \"Emma\"\n    )\n\n    val numbers = createPair(\n        10,\n        20\n    )\n\n    println(names)\n\n    println(numbers)\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>(Alex, Emma)\n(10, 20)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Kotlin may infer a common supertype when arguments have different types.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you need to enforce a particular type, provide an explicit type argument.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">6. Kotlin Generics with Multiple Type Parameters<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A generic class can declare more than one type parameter.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>data class KeyValue&lt;K, V&gt;(\n    val key: K,\n    val value: V\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Usage<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() {\n\n    val user = KeyValue(\n        key = 101,\n        value = \"Alex\"\n    )\n\n    val setting = KeyValue(\n        key = \"dark_mode\",\n        value = true\n    )\n\n    println(user)\n\n    println(setting)\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>KeyValue(key=101, value=Alex)\nKeyValue(key=dark_mode, value=true)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>K<\/code> represents the key type.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>V<\/code> represents the value type.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Both can be different.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This concept is also used in Kotlin&#8217;s <code>Map&lt;K, V&gt;<\/code> interface.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">7. Kotlin Generic Interfaces<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Interfaces can also use type parameters.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>interface Repository&lt;T&gt; {\n\n    fun save(item: T)\n\n    fun getAll(): List&lt;T&gt;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Implementation<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>data class User(\n    val id: Int,\n    val name: String\n)\n\nclass UserRepository : Repository&lt;User&gt; {\n\n    private val users = mutableListOf&lt;User&gt;()\n\n    override fun save(item: User) {\n        users.add(item)\n    }\n\n    override fun getAll(): List&lt;User&gt; {\n        return users.toList()\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Usage<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() {\n\n    val repository = UserRepository()\n\n    repository.save(\n        User(1, \"Alex\")\n    )\n\n    repository.save(\n        User(2, \"Emma\")\n    )\n\n    println(repository.getAll())\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;User(id=1, name=Alex), User(id=2, name=Emma)]<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The generic interface defines a reusable contract.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The implementation specifies which type it supports.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This pattern is useful when designing reusable data layers.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">8. Kotlin Generic Type Constraints<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Sometimes we want to restrict which types can be used with a generic function or class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Kotlin supports type constraints.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun &lt;T : Number&gt; process(value: T) {\n    println(value)\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here, <code>T<\/code> must be a subtype of <code>Number<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun &lt;T : Number&gt; printNumber(\n    value: T\n) {\n\n    println(value.toDouble())\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Usage<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() {\n\n    printNumber(10)\n\n    printNumber(15.5)\n\n    printNumber(20L)\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>10.0\n15.5\n20.0<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Invalid Usage<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Compilation error\n\nprintNumber(\"Hello\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">String is not a subtype of Number.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why Use Constraints?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Constraints are useful when generic code requires specific capabilities from its type arguments.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">9. Multiple Constraints Using where<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Kotlin supports multiple type constraints using the <code>where<\/code> keyword.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun &lt;T&gt; printComparable(\n    value: T\n) where T : Number,\n        T : Comparable&lt;T&gt; {\n\n    println(value)\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Usage<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() {\n\n    printComparable(10)\n\n    printComparable(15.5)\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The generic type must satisfy both constraints.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It must be:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A subtype of Number<\/li>\n\n\n\n<li>Comparable with values of its own type<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Practical Use<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Multiple constraints are useful when designing generic utilities that require more than one interface or capability.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">10. Understanding Variance in Kotlin<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Variance describes how relationships between types affect relationships between generic types.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Consider:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>open class Animal\n\nclass Dog : Animal()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A Dog is an Animal.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But does that mean a generic container of Dogs can always be used as a container of Animals?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Not necessarily.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The answer depends on the generic type&#8217;s variance.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Kotlin supports three important concepts:<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>Invariance<\/li>\n\n\n\n<li>Covariance<\/li>\n\n\n\n<li>Contravariance<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s understand each one.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">11. Kotlin Invariance<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">By default, user-defined generic type parameters are invariant.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>open class Animal\n\nclass Dog : Animal()\n\nclass Box&lt;T&gt;(\n    var value: T\n)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now consider:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>val dogBox: Box&lt;Dog&gt; = Box(Dog())<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The following assignment is invalid:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Compilation error\n\nval animalBox: Box&lt;Animal&gt; = dogBox<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Why?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If the assignment were allowed, we could insert another Animal subtype into a Box intended only for Dogs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Invariance prevents this unsafe behavior.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Key Point<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For an invariant generic class:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Box&lt;Dog&gt;<\/code> is not a subtype of <code>Box&lt;Animal&gt;<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">12. Kotlin Covariance Using out<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Covariance allows a generic type to preserve a subtype relationship.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Kotlin uses the <code>out<\/code> keyword for declaration-site covariance.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>open class Animal\n\nclass Dog : Animal()\n\nclass Producer&lt;out T&gt;(\n    private val value: T\n) {\n\n    fun get(): T {\n        return value\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Usage<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() {\n\n    val dogProducer: Producer&lt;Dog&gt; =\n        Producer(Dog())\n\n    val animalProducer: Producer&lt;Animal&gt; =\n        dogProducer\n\n    println(animalProducer.get())\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>out<\/code> keyword indicates that T is used in permitted output positions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Producer exposes values of type T without accepting arbitrary T values from callers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Therefore, a Producer of Dogs can safely be used where a Producer of Animals is expected.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Practical Example<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Kotlin&#8217;s read-only List interface is covariant.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>val dogs: List&lt;Dog&gt; = listOf(\n    Dog(),\n    Dog()\n)\n\nval animals: List&lt;Animal&gt; = dogs<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This is valid.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Covariance does not mean every operation is allowed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The compiler restricts how a covariant type parameter can be used to preserve type safety.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">13. Kotlin Contravariance Using in<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Contravariance reverses the subtype relationship for a generic type.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Kotlin uses the <code>in<\/code> keyword.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>open class Animal\n\nclass Dog : Animal()\n\nclass Consumer&lt;in T&gt; {\n\n    fun consume(value: T) {\n        println(\"Consumed: $value\")\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Usage<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() {\n\n    val animalConsumer: Consumer&lt;Animal&gt; =\n        Consumer()\n\n    val dogConsumer: Consumer&lt;Dog&gt; =\n        animalConsumer\n\n    dogConsumer.consume(Dog())\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The Consumer accepts values of type T.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A Consumer capable of handling any Animal can also handle a Dog.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Therefore, a Consumer of Animals can be used where a Consumer of Dogs is expected.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Key Rule<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>in<\/code> is commonly associated with consuming values.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>out<\/code> is commonly associated with producing values.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">14. Covariance vs Contravariance<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s compare the two concepts.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Feature<\/th><th>Covariance<\/th><th>Contravariance<\/th><\/tr><tr><td>Keyword<\/td><td><code>out<\/code><\/td><td><code>in<\/code><\/td><\/tr><tr><td>Main role<\/td><td>Producer<\/td><td>Consumer<\/td><\/tr><tr><td>Type relationship<\/td><td>Preserved<\/td><td>Reversed<\/td><\/tr><tr><td>Typical example<\/td><td>Read-only data source<\/td><td>Value processor<\/td><\/tr><tr><td>Common use<\/td><td>Returning values<\/td><td>Accepting values<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Easy Way to Remember<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Producer = out<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Consumer = in<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is often summarized using the principle:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Producer Extends, Consumer Super (PECS)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The phrase comes from Java generics, but the producer-consumer idea is useful when learning Kotlin variance.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">15. Kotlin Use-Site Variance<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Kotlin also supports variance annotations at the point where a generic type is used.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is called use-site variance.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun copyAnimals(\n    source: Array&lt;out Animal&gt;,\n    destination: Array&lt;Animal&gt;\n) {\n\n    for (index in source.indices) {\n        destination&#91;index] = source&#91;index]\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Usage<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>val dogs = arrayOf(\n    Dog(),\n    Dog()\n)\n\nval animals = arrayOf&lt;Animal&gt;(\n    Animal(),\n    Animal()\n)\n\ncopyAnimals(dogs, animals)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Kotlin arrays are invariant because they support both reading and writing.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The projection:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Array&lt;out Animal&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">allows us to safely read Animal values from an array whose elements may be a subtype of Animal.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This example assumes the destination array is large enough.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Production code should validate sizes or use an API that handles mismatched collection lengths safely.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">16. Kotlin Star Projections<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Sometimes we need to work with a generic type without knowing its exact type argument.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Kotlin provides star projections using <code>*<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun printItems(\n    items: List&lt;*&gt;\n) {\n\n    items.forEach {\n        println(it)\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Usage<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() {\n\n    printItems(\n        listOf(\"Alex\", \"Emma\")\n    )\n\n    printItems(\n        listOf(10, 20, 30)\n    )\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The type:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;*&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">represents a List whose element type is not known at the call site.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Values can be read safely as nullable Any values.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">When to Use Star Projections<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Star projections are useful when:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The exact generic type is unknown<\/li>\n\n\n\n<li>Inspecting generic collections<\/li>\n\n\n\n<li>Working with reflection APIs<\/li>\n\n\n\n<li>Handling heterogeneous data structures<\/li>\n\n\n\n<li>Building type-independent utilities<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Star projections preserve type safety by restricting operations that would require knowledge of the missing type argument.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">They are not equivalent to disabling type checking.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">17. Kotlin Type Erasure<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Kotlin generics on the JVM are generally implemented using type erasure.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This means generic type arguments are not always available at runtime.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun &lt;T&gt; printType(\n    value: T\n) {\n    println(value?.let { it::class.simpleName })\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This can inspect the runtime class of a non-null value.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, it does not preserve all nested generic type information.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Invalid Generic Type Check<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun &lt;T&gt; checkList(value: Any) {\n\n    \/\/ Not allowed:\n    \/\/ if (value is List&lt;T&gt;) { }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The runtime generally cannot determine whether an arbitrary List contains elements of type T from its erased type argument alone.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What Can We Check?<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun checkList(value: Any) {\n\n    if (value is List&lt;*&gt;) {\n        println(\"This is a List\")\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This checks whether the object is a List without claiming to know its element type.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">18. Kotlin Reified Type Parameters<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Kotlin provides reified type parameters for inline functions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>reified<\/code> keyword allows certain operations involving the type parameter at runtime.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>inline fun &lt;reified T&gt; isType(\n    value: Any\n): Boolean {\n\n    return value is T\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Usage<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() {\n\n    println(isType&lt;String&gt;(\"Kotlin\"))\n\n    println(isType&lt;Int&gt;(\"Kotlin\"))\n\n    println(isType&lt;Int&gt;(100))\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>true\nfalse\ntrue<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The function is declared with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>inline fun &lt;reified T&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This allows the compiler to make the type available for supported checks at the call site.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Reified type parameters do not eliminate every limitation of JVM type erasure.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, checking whether an object is a <code>List&lt;String&gt;<\/code> does not generally verify the runtime type of every element.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">19. Practical Android Example: Generic API Result Wrapper<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Android applications frequently communicate with remote APIs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A reusable result wrapper can represent success, failure, and loading states.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Create the Generic Result Type<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sealed interface ApiResult&lt;out T&gt; {\n\n    data class Success&lt;T&gt;(\n        val data: T\n    ) : ApiResult&lt;T&gt;\n\n    data class Error(\n        val message: String\n    ) : ApiResult&lt;Nothing&gt;\n\n    data object Loading : ApiResult&lt;Nothing&gt;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The generic parameter T represents the success data type.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>out<\/code> keyword allows covariance.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>Nothing<\/code> type makes Error and Loading compatible with ApiResult values of different success types.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Create a User Model<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>data class User(\n    val id: Int,\n    val name: String\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Create a Repository Function<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun getUser(): ApiResult&lt;User&gt; {\n\n    return ApiResult.Success(\n        User(\n            id = 101,\n            name = \"Alex\"\n        )\n    )\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Handle the Result<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() {\n\n    when (val result = getUser()) {\n\n        is ApiResult.Success -&gt; {\n            println(result.data.name)\n        }\n\n        is ApiResult.Error -&gt; {\n            println(result.message)\n        }\n\n        ApiResult.Loading -&gt; {\n            println(\"Loading...\")\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Alex<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Why Is This Useful?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The same ApiResult type can represent:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ApiResult&lt;User&gt;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>ApiResult&lt;List&lt;Product&gt;&gt;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>ApiResult&lt;String&gt;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>ApiResult&lt;Boolean&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This reduces duplicate result-handling classes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Production Note<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For real networking code, handle exceptions, cancellation, HTTP status codes, error bodies, and authentication failures appropriately.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Loading is often better represented as part of a UI state model or asynchronous state stream rather than as the direct return value of a synchronous repository function.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">20. Practical Android Example: Generic Repository Pattern<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s create a simple generic repository.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Define the Repository Interface<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>interface BaseRepository&lt;T, ID&gt; {\n\n    fun insert(item: T)\n\n    fun getById(id: ID): T?\n\n    fun getAll(): List&lt;T&gt;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Create a User Model<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>data class User(\n    val id: Int,\n    val name: String\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Implement the Repository<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class InMemoryUserRepository :\n    BaseRepository&lt;User, Int&gt; {\n\n    private val users = mutableListOf&lt;User&gt;()\n\n    override fun insert(item: User) {\n        users.add(item)\n    }\n\n    override fun getById(id: Int): User? {\n        return users.find {\n            it.id == id\n        }\n    }\n\n    override fun getAll(): List&lt;User&gt; {\n        return users.toList()\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Usage<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() {\n\n    val repository = InMemoryUserRepository()\n\n    repository.insert(\n        User(1, \"Alex\")\n    )\n\n    repository.insert(\n        User(2, \"Emma\")\n    )\n\n    println(repository.getById(1))\n\n    println(repository.getAll())\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>User(id=1, name=Alex)\n&#91;User(id=1, name=Alex), User(id=2, name=Emma)]<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The generic repository interface defines reusable operations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The implementation provides type-specific behavior.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Production Note<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This example uses in-memory storage.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Real Android repositories may interact with Room, Retrofit, DataStore, or other data sources.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A generic repository is not always the best architecture. Avoid forcing unrelated entities into a single abstraction when their data operations differ significantly.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">21. Jetpack Compose Mini-Project: Generic Data List<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s build a reusable Jetpack Compose list component using Kotlin Generics.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Our component will support:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Different data types<\/li>\n\n\n\n<li>Reusable LazyColumn rendering<\/li>\n\n\n\n<li>Type-safe item handling<\/li>\n\n\n\n<li>Custom item content<\/li>\n\n\n\n<li>Stable item keys<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Create the Generic Composable<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@Composable\nfun &lt;T&gt; GenericList(\n    items: List&lt;T&gt;,\n    key: (T) -&gt; Any,\n    itemContent: @Composable (T) -&gt; Unit\n) {\n\n    LazyColumn(\n        verticalArrangement = Arrangement.spacedBy(12.dp)\n    ) {\n\n        items(\n            items = items,\n            key = key\n        ) { item -&gt;\n\n            itemContent(item)\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The type parameter T represents the item type.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The component receives:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A List of items<\/li>\n\n\n\n<li>A function that generates stable keys<\/li>\n\n\n\n<li>A composable lambda that renders each item<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This allows the same list component to display different models.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Create User and Product Models<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>data class DemoUser(\n    val id: Int,\n    val name: String,\n    val email: String\n)\n\ndata class DemoProduct(\n    val id: Int,\n    val name: String,\n    val price: Int\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Create Sample Data<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun getUsers(): List&lt;DemoUser&gt; {\n\n    return listOf(\n        DemoUser(1, \"Alex\", \"alex@example.com\"),\n        DemoUser(2, \"Emma\", \"emma@example.com\"),\n        DemoUser(3, \"John\", \"john@example.com\")\n    )\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>fun getProducts(): List&lt;DemoProduct&gt; {\n\n    return listOf(\n        DemoProduct(1, \"Android Phone\", 15000),\n        DemoProduct(2, \"Bluetooth Mouse\", 800),\n        DemoProduct(3, \"Keyboard\", 2500)\n    )\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Create the Demo Screen<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@Composable\nfun GenericsDemoScreen() {\n\n    var showUsers by rememberSaveable {\n        mutableStateOf(true)\n    }\n\n    val users = remember {\n        getUsers()\n    }\n\n    val products = remember {\n        getProducts()\n    }\n\n    Column(\n        modifier = Modifier\n            .fillMaxSize()\n            .padding(16.dp),\n        verticalArrangement = Arrangement.spacedBy(16.dp)\n    ) {\n\n        Text(\n            text = \"Kotlin Generics Demo\",\n            style = MaterialTheme.typography.headlineMedium\n        )\n\n        Row(\n            horizontalArrangement = Arrangement.spacedBy(12.dp)\n        ) {\n\n            Button(\n                onClick = {\n                    showUsers = true\n                }\n            ) {\n                Text(\"Users\")\n            }\n\n            Button(\n                onClick = {\n                    showUsers = false\n                }\n            ) {\n                Text(\"Products\")\n            }\n        }\n\n        if (showUsers) {\n\n            GenericList(\n                items = users,\n                key = { it.id }\n            ) { user -&gt;\n\n                Card(\n                    modifier = Modifier.fillMaxWidth()\n                ) {\n\n                    Column(\n                        modifier = Modifier.padding(16.dp)\n                    ) {\n\n                        Text(\n                            text = user.name,\n                            style = MaterialTheme.typography.titleMedium\n                        )\n\n                        Text(\n                            text = user.email\n                        )\n                    }\n                }\n            }\n\n        } else {\n\n            GenericList(\n                items = products,\n                key = { it.id }\n            ) { product -&gt;\n\n                Card(\n                    modifier = Modifier.fillMaxWidth()\n                ) {\n\n                    Column(\n                        modifier = Modifier.padding(16.dp)\n                    ) {\n\n                        Text(\n                            text = product.name,\n                            style = MaterialTheme.typography.titleMedium\n                        )\n\n                        Text(\n                            text = \"\u20b9${product.price}\"\n                        )\n                    }\n                }\n            }\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Required Imports<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import androidx.compose.foundation.layout.Arrangement\nimport androidx.compose.foundation.layout.Column\nimport androidx.compose.foundation.layout.Row\nimport androidx.compose.foundation.layout.fillMaxSize\nimport androidx.compose.foundation.layout.fillMaxWidth\nimport androidx.compose.foundation.layout.padding\nimport androidx.compose.foundation.lazy.LazyColumn\nimport androidx.compose.foundation.lazy.items\nimport androidx.compose.material3.Button\nimport androidx.compose.material3.Card\nimport androidx.compose.material3.MaterialTheme\nimport androidx.compose.material3.Text\nimport androidx.compose.runtime.Composable\nimport androidx.compose.runtime.getValue\nimport androidx.compose.runtime.mutableStateOf\nimport androidx.compose.runtime.remember\nimport androidx.compose.runtime.saveable.rememberSaveable\nimport androidx.compose.runtime.setValue\nimport androidx.compose.ui.Modifier\nimport androidx.compose.ui.unit.dp<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Expected UI<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>+--------------------------------+\n|                                |\n|      Kotlin Generics Demo      |\n|                                |\n|   &#91; Users ]    &#91; Products ]    |\n|                                |\n| +----------------------------+ |\n| | Alex                       | |\n| | alex@example.com           | |\n| +----------------------------+ |\n|                                |\n| +----------------------------+ |\n| | Emma                       | |\n| | emma@example.com           | |\n| +----------------------------+ |\n|                                |\n| +----------------------------+ |\n| | John                       | |\n| | john@example.com           | |\n| +----------------------------+ |\n|                                |\n+--------------------------------+<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When the user taps Products, the same GenericList component displays product information.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How It Works<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The GenericList composable accepts any supported item type.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For users:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GenericList(\n    items = users,\n    key = { it.id }\n) { user -&gt;\n    Text(user.name)\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For products:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GenericList(\n    items = products,\n    key = { it.id }\n) { product -&gt;\n    Text(product.name)\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The compiler infers the correct type in each case.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Benefits<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Reusable UI components<\/li>\n\n\n\n<li>Type-safe rendering<\/li>\n\n\n\n<li>Reduced duplication<\/li>\n\n\n\n<li>Customizable item layouts<\/li>\n\n\n\n<li>Cleaner Compose architecture<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Production Note<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A generic list is useful when it provides meaningful reuse.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, specialized composables are often preferable when screens have different layout, behavior, accessibility, or performance requirements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Avoid unnecessary abstraction.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">22. Common Kotlin Generics Mistakes<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 1: Assuming All Generic Types Are Covariant<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This is invalid for invariant types:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>val dogs: MutableList&lt;Dog&gt; = mutableListOf()\n\n\/\/ Compilation error\n\/\/ val animals: MutableList&lt;Animal&gt; = dogs<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">MutableList is invariant because it supports both reading and writing.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 2: Using out for Input Positions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A type parameter declared with <code>out<\/code> cannot be used freely in input positions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The compiler restricts unsafe operations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 3: Using in for Output Positions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A contravariant type parameter cannot be freely exposed as a return type.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 4: Assuming Generic Types Are Fully Available at Runtime<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">JVM type erasure limits runtime access to generic type arguments.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use reified parameters or explicit type tokens when appropriate.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 5: Overengineering Generic Repositories<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Not every data source needs a generic repository.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Sometimes a specific repository interface is clearer.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 6: Using Any Instead of Generics<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Avoid replacing type-safe generic APIs with unnecessary Any-based implementations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Generics preserve more useful compile-time information.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 7: Ignoring Nullability<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Generic type parameters can represent nullable or non-nullable types depending on their constraints and usage.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Be explicit when nullability matters.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">23. Kotlin Generics Best Practices<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Follow these recommendations:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use generics when multiple types genuinely share behavior.<\/li>\n\n\n\n<li>Prefer descriptive type parameter names when helpful.<\/li>\n\n\n\n<li>Use constraints to express required capabilities.<\/li>\n\n\n\n<li>Understand covariance and contravariance.<\/li>\n\n\n\n<li>Use <code>out<\/code> for producer-oriented APIs.<\/li>\n\n\n\n<li>Use <code>in<\/code> for consumer-oriented APIs.<\/li>\n\n\n\n<li>Avoid unnecessary casts.<\/li>\n\n\n\n<li>Prefer type-safe interfaces.<\/li>\n\n\n\n<li>Use reified type parameters only when appropriate.<\/li>\n\n\n\n<li>Avoid excessive generic abstraction.<\/li>\n\n\n\n<li>Keep repository contracts aligned with actual domain requirements.<\/li>\n\n\n\n<li>Write tests for reusable generic components.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">These practices help build scalable and maintainable Kotlin applications.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">24. Kotlin Generics Quick Reference<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Concept<\/th><th>Example<\/th><th>Purpose<\/th><\/tr><tr><td>Generic class<\/td><td><code>class Box&lt;T&gt;<\/code><\/td><td>Reusable class<\/td><\/tr><tr><td>Generic function<\/td><td><code>fun &lt;T&gt; identity(value: T)<\/code><\/td><td>Reusable function<\/td><\/tr><tr><td>Multiple types<\/td><td><code>class PairBox&lt;K, V&gt;<\/code><\/td><td>Multiple type parameters<\/td><\/tr><tr><td>Type constraint<\/td><td><code>&lt;T : Number&gt;<\/code><\/td><td>Restrict allowed types<\/td><\/tr><tr><td>Multiple constraints<\/td><td><code>where T : Number, T : Comparable&lt;T&gt;<\/code><\/td><td>Require multiple capabilities<\/td><\/tr><tr><td>Covariance<\/td><td><code>out T<\/code><\/td><td>Producer-oriented type<\/td><\/tr><tr><td>Contravariance<\/td><td><code>in T<\/code><\/td><td>Consumer-oriented type<\/td><\/tr><tr><td>Star projection<\/td><td><code>List&lt;*&gt;<\/code><\/td><td>Unknown type argument<\/td><\/tr><tr><td>Reified parameter<\/td><td><code>inline fun &lt;reified T&gt;<\/code><\/td><td>Supported runtime type operations<\/td><\/tr><tr><td>Generic interface<\/td><td><code>interface Repository&lt;T&gt;<\/code><\/td><td>Reusable contract<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">25. Practical Exercises<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Try these exercises to improve your understanding of Kotlin Generics.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 1: Generic Box<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a generic Box class that stores and returns a value.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Test it with String, Int, and Boolean.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 2: Generic Function<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a generic function that prints the first element of a List.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Handle empty lists safely.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 3: Multiple Type Parameters<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a generic KeyValue class that stores different key and value types.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 4: Generic Constraint<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a function that accepts only Number subtypes and converts the input to Double.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 5: Covariance<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a Producer interface using <code>out T<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Demonstrate assigning a producer of a subtype to a producer of its supertype.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 6: Contravariance<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a Consumer interface using <code>in T<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Demonstrate a consumer accepting values of a subtype.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 7: Generic API Result<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a generic sealed interface with Success, Error, and Loading states.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 8: Generic Repository<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a repository interface that supports inserting and retrieving different model types.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 9: Jetpack Compose Mini-Project<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Build a reusable generic LazyColumn component that displays:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>User information<\/li>\n\n\n\n<li>Product information<\/li>\n\n\n\n<li>Custom item layouts<\/li>\n\n\n\n<li>Stable item keys<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">26. Frequently Asked Questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">What are Generics in Kotlin?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Generics allow classes, interfaces, and functions to work with different types while preserving compile-time type safety.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What does T mean in Kotlin Generics?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">T is a conventional name for a type parameter.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Other names such as E, K, V, ID, or ResultType can also be used.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is covariance in Kotlin?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Covariance allows a generic type to preserve a subtype relationship.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is declared using <code>out<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is contravariance in Kotlin?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Contravariance reverses the subtype relationship for a generic type.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is declared using <code>in<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is the difference between in and out?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>out<\/code> is commonly used for types that produce values.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>in<\/code> is commonly used for types that consume values.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is a star projection?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A star projection represents a generic type whose exact type argument is unknown.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It allows restricted, type-safe operations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is a reified type parameter?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A reified type parameter is declared in an inline function and allows supported runtime operations involving the type parameter.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Are Generics used in Jetpack Compose?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Generics are used throughout Kotlin and Compose APIs, including collections, state containers, lazy lists, and reusable composables.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What We&#8217;ve Learned<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In this tutorial, we learned:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Kotlin Generics<\/li>\n\n\n\n<li>Generic classes<\/li>\n\n\n\n<li>Generic functions<\/li>\n\n\n\n<li>Multiple type parameters<\/li>\n\n\n\n<li>Generic interfaces<\/li>\n\n\n\n<li>Type constraints<\/li>\n\n\n\n<li>Multiple constraints using where<\/li>\n\n\n\n<li>Invariance<\/li>\n\n\n\n<li>Covariance using out<\/li>\n\n\n\n<li>Contravariance using in<\/li>\n\n\n\n<li>Use-site variance<\/li>\n\n\n\n<li>Star projections<\/li>\n\n\n\n<li>Type erasure<\/li>\n\n\n\n<li>Reified type parameters<\/li>\n\n\n\n<li>Generic API result wrappers<\/li>\n\n\n\n<li>Generic repository patterns<\/li>\n\n\n\n<li>Reusable Jetpack Compose lists<\/li>\n\n\n\n<li>Common mistakes<\/li>\n\n\n\n<li>Best practices<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Kotlin Generics are essential for building reusable, type-safe, and maintainable applications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">They play an important role in modern Android architecture, including repositories, networking, state management, and Jetpack Compose UI components.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Understanding Generics will help you design flexible APIs while avoiding unnecessary duplication.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In our next tutorial, we&#8217;ll explore:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Kotlin Exception Handling Explained: try, catch, finally, throw &amp; Result<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You&#8217;ll learn:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>What exceptions are<\/li>\n\n\n\n<li>try and catch<\/li>\n\n\n\n<li>finally<\/li>\n\n\n\n<li>throw<\/li>\n\n\n\n<li>Custom exceptions<\/li>\n\n\n\n<li>Kotlin Result<\/li>\n\n\n\n<li>runCatching<\/li>\n\n\n\n<li>getOrNull and getOrElse<\/li>\n\n\n\n<li>Exception handling in coroutines<\/li>\n\n\n\n<li>Android networking error handling<\/li>\n\n\n\n<li>Best practices for production applications<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Happy Coding!<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Continue learning modern Android development with <strong>Moiks Developer Blog<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Master Kotlin generics with practical examples. Learn generic classes, functions, type constraints, covariance, contravariance, star projections, and reusable Android repository patterns.<\/p>\n","protected":false},"author":1,"featured_media":143,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0,"footnotes":""},"categories":[3,333,5],"tags":[],"class_list":["post-142","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android-development","category-android-tutorials","category-kotlin"],"_links":{"self":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts\/142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/comments?post=142"}],"version-history":[{"count":1,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts\/142\/revisions"}],"predecessor-version":[{"id":144,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts\/142\/revisions\/144"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/media\/143"}],"wp:attachment":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/media?parent=142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/categories?post=142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/tags?post=142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}