Given the following method extensions (JsonResult is just a wrapper with some additional fields):
fun <T, R> T.toJson(transform: (T) -> R) = JsonResult(transform(this))
fun <T, R> List<T>.toJson(transform: (T) -> R) = JsonResult(this.map(transform))
fun <T, R> Page<T>.toJson(transform: (T) -> R) = JsonResult(this.content.map(transform)
When I try to call any of the above methods, either from a list or an object that is not a list, I get a compiler error with the message Cannot choose among the following candidates without completing type inference
which I expect
import org.springframework.data.domain.Page
val person = Person()
person.toJson { } // compiler error
val people = List<Person>()
people.toJson { } // compiler error
val pageablePeople = Page<Person>
pageablePeople.toJson { }
I wonder if there is any other (idiomatic?) way to declare those functions without changing their names.