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

Time:11-05

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?

CodePudding user response:

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

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