Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not enough information to infer parameter T

Tags:

kotlin

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?

like image 861
José Nobre Avatar asked Apr 14 '19 23:04

José Nobre


1 Answers

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"
like image 189
Todd Avatar answered Sep 21 '22 07:09

Todd