Context
: I am building a REST API with kotlin using Spring
Problem
: I have a Kotlin class called Response
that accepts a generic like this:
class Response<T> {
var data: T? = null
var dataArray: List<T>? = null
var errors: List<String>? = null
get() {
if (field == null) {
this.errors = ArrayList()
}
return field
}
}
When I try to instantiate in one of my API Controllers
like this:
val response = Response()
response.setData(someting)
It gives me Not enough information to infer parameter T
.
How can I avoid this error?
You will have to specify what T
is in this case. Supposing it is a String
, you could do it like this:
val response = Response<String>()
response.data = "Something that is a String"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With