https://kotlinlang.org logo
#announcements
Title
# announcements
j

Jakub Gwóźdź

11/18/2019, 8:13 AM
Good morning. I have this stupid problem with generics-vs-overloading-vs-varargs in JOOQ. While following code:
Copy code
public class GenericsJava {
  public static Condition testJooqGenerics(Table<?> table, List<Object> list) {
    Field<Object> myColumn = table.field("MY_COLUMN", Object.class);
    return <http://myColumn.in|myColumn.in>(list);
  }
}
works well in Java, the Kotlin version
Copy code
object GenericsKotlin {
    fun testJooqGenerics(table: Table<*>, list: List<Any?>?): Condition {
        val myColumn: Field<Any> = table.field("MY_COLUMN", Any::class.java)
        return myColumn.`in`(list)
    }
}
yelds Overload resolution ambiguity:
Copy code
@Support public abstract fun `in`(vararg p0: Any!): Condition! defined in org.jooq.Field
@Support public abstract fun `in`(p0: (Mutable)Collection<*>!): Condition! defined in org.jooq.Field
Is there any way to work around this API issue?
(obviously, when the type is anything but
Any
, e.g.
Field<String>
, it works great)
h

henrik

11/18/2019, 9:12 AM
Can you add your import statements?
j

Jakub Gwóźdź

11/18/2019, 9:18 AM
sure, it's
Copy code
package test.generics

import org.jooq.Condition
import org.jooq.Field
import org.jooq.Table
plus java.util.List and semicolons in java version 🙂
Ok, I found the solution in case anyone needs it sometime in the future: reified generics.
Copy code
object GenericsKotlin {
    inline fun <reified T> testJooqGenericsInline(table: Table<*>, list: List<T>): Condition {
        val myColumn: Field<T> = table.field("MY_COLUMN", T::class.java)
        return myColumn.`in`(list)
    }

    fun testJooqGenerics(table: Table<*>, list: List<Any>): Condition {
        return testJooqGenericsInline(table, list)
    }
}
2 Views