Hi.
I ran into a problem recently while upgrading dependent libraries where a new function was added that overloaded one I was using. The compiler started throwing a compile time error.
Given this function
fun <T> executeWithNull(action: (ofy: SomeObject) -> T): T? {
val callable = Callable {
return@Callable SomeObjectService.run {
val value = SomeObjectService.value()
action(value)
}
}
return this.timer.time(callable)
}
Where the SomeObjectService has a function run, as below, all works. I’m assuming Kotlin is building an object with a single method in order to map the lambda to the method parameter below.
public static <R> R run(final Work<R> work) {
try (Closeable closeable = begin()) {
return work.run();
}
}
Now I add another run method to the SomeObjectService and Kotlin throws a compile time error.
So adding
public static void run(final Runnable work) {
factory().run(work);
}
inferred type is Unit! but T? was expected
I was hoping to understand if this is a Kotlin compiler “bug” or if I’m just expecting to much (Mapping to the correct method based the return type of the lambda supplied) or if I’m just not understanding the issue at all.
PS wrapping the lambda passed to the function in a new object (Work) works.