Statically resolved functions

Hi. Why comp function is not resolved statically like compose extension?

infix fun <A, B, C> ((B) -> C).compose(g: (A) -> B): (A) -> C =
  { x: A -> this(g(x)) }


fun <A, B, C>comp(f: (B) -> C): ((A) -> B) -> (A) -> C =
  { g -> { x -> f(g(x)) } }
    
val add = {a: Int -> {b: Int -> a + b }}

fun main() {
  val f1 = add(2) compose add(2)
    
  val f2 = comp(add(2))(add(2)) // error: Not enough information to infer type variable A

  println(f1(0))
  println(f2(0))
}

It looks like Kotlin does not analyse arguments in EXPR.method(args) to infer the type of EXPR. In your case, method is the invoke operator method. Here’s a simpler, similar case that fails :

fun <T> getPrintln(): (T) -> Unit = { println(it) } 
fun main() {
    getPrintln()(5)
}
1 Like