Function binding fails depending on the return type of a lambda parameter


#1

Hi,

given the following declarations

  @JvmName("bindCollection")
  inline fun <reified T> bind(crossinline fn: () -> Collection<T>) {
  // ...
  }

  @JvmName("bindFuture")
  inline fun <reified T> bind(crossinline fn: () -> Future<T>) {
  // ...
  }

// ...
bind { map.values } // <<-- compiler error here

Compiler fails with “Cannot choose among the following candidates without completing type inference”:

However, the following works fine:

  @JvmName("bindCollection")
  inline fun <reified T> bind(crossinline fn: () -> Collection<T>) {
  // ...
  }

  @JvmName("bind")
  inline fun <reified T> bind(crossinline fn: () -> T) {
  // ...
  }

// ...
bind { map.values } // compiles fine

Is this by design?

thanks and regards
Fuzz


#2

This is the known problem https://youtrack.jetbrains.com/issue/KT-11265, hopefully it might get fixed in some of the next versions of Kotlin.


#3

Many thanks Ilya.

Does this mean that the compiler finds ambiguity between () -> Collection<T> and () -> Future<T> because it matches on the category “function returning a single parameter generic class?

If so, is the YT issue specific enough with respect to parameterisation of generics?

Thanks and kind regards
Fuzz