Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic Data class in Kotlin

Create a generic data class which has one variable data Type

data class <T> GenericResponse(
  val success: Boolean,
  val message: String,
  val data: T
)

To use it: GenericResponse<SomeOtherDataClass>

How to do this in kotlin?

like image 213
Trixy Avatar asked Feb 01 '26 02:02

Trixy


1 Answers

Just put the <T> after the class name:

data class GenericResponse<T>(
  val success: Boolean,
  val message: String,
  val data: T
)
like image 183
Joffrey Avatar answered Feb 02 '26 19:02

Joffrey