java - How to declare a class that contains a field with generic type in Kotlin? -
in kotlin have data class.
data class apiresponse<out t>(val status: string, val code: int, val message: string, val data: t?)
i want declare class include this:
class apierror(message: string, response: apiresponse) : exception(message) {}
but kotlin giving error: 1 type argument expected class apiresponse defined in com.mypackagename
in java can this:
class apierror extends exception { apiresponse response; public apierror(string message, apiresponse response) { super(message); this.response = response; } }
how can convert code kotlin?
what have in java raw type. in section on star-projections, kotlin documentation says:
note: star-projections java's raw types, safe.
they describe use-case:
sometimes want know nothing type argument, still want use in safe way. safe way here define such projection of generic type, every concrete instantiation of generic type subtype of projection.
your apierror
class therefore becomes:
class apierror(message: string, val response: apiresponse<*>) : exception(message) {}
Comments
Post a Comment