Search code examples
androidkotlinextension-methods

How can I used a generic extension function in Kotlin for two type of data class mapper


I have two data model classes and I want to use a generic extension function to map these classes and other classes.

This is a sample of my code to map the two classes:

fun HomeRequestBodyWithAuthQuery.Client.graphToDomain(): Client =
    Client(id = this.id, givenName = this.givenName)

I wrote this extension function but the return value has an error:

fun <G,  T> G.graphqlToDomain(): T  = T

enter image description here how can I write a kotlin extension function for this work?


Solution

  • You can take your generic type as a parameter and return it.

    fun <G, T> G.graphqlToDomain(t: T): T = t