0
class DATA<T>

public  fun <T> add1(t1:T,t2:T): List<T> =  listOf(t1, t2)

public  infix fun <T> T.add2(t2:T): List<T> =  listOf(this, t2)

fun printData(list: List<DATA<String>>) {
    println("Hello World!")
}

fun main(args: Array<String>) {
    printData( add1(DATA(), DATA()) )
    printData( DATA() add2 DATA())    //why this line can not compile?
}

As the above code shows, when I using infix function, the type can't be deduced properly, and produced a compile error, why kotlin does not support this?

lxw
  • 21
  • 3
  • I'm just taking a guess, but maybe what's happening is that with the second function, the compiler has to determine the type of the first object before it can even determine what extension functions are available to call on it. With the first function, this step is not necessary. I don't think it makes a difference whether the function is `infix` or not. The problem is caused by it being an extension function. – Tenfour04 Mar 12 '20 at 17:19
  • Does this answer your question? [Extension functions for generic classes in Kotlin](https://stackoverflow.com/questions/32883936/extension-functions-for-generic-classes-in-kotlin) – ordonezalex Mar 12 '20 at 17:47

0 Answers0