Home > Enterprise >  Parametrized data class with variance
Parametrized data class with variance

Time:09-22

I have such class

data class MyClass<T, out R>(
    val data: T? = null,
    val error: R? = null
)

But actually I want my error to extend Throwable, for example. How should I declare MyClass for this purpose?

CodePudding user response:

Just add Throwable constraint to R (see documentation):

data class MyClass<T, out R : Throwable>(
    val data: T? = null,
    val error: R? = null
)

  • Related