José Nobre
José Nobre

Reputation: 789

Not enough information to infer parameter T

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?

Upvotes: 6

Views: 11523

Answers (1)

Todd
Todd

Reputation: 31660

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"

Upvotes: 13

Related Questions