Is it possible to make reference to generic functions in Kotlin?
For example, let's say I have a function:
fun inline <reified T> appendToString(a: T, b: T) = a.toString() + b.toString
How can you reference this function? This will not compile
var a = ::appendToString<Int>
Generic functions are functions declared with one or more generic type parameters. They may be methods in a class or struct , or standalone functions. A single generic declaration implicitly declares a family of functions that differ only in the substitution of a different actual type for the generic type parameter.
Generic functions (objects from or extending class genericFunction ) are extended function objects, containing information used in creating and dispatching methods for this function. They also identify the package associated with the function and its methods.
Generic code enables you to write flexible, reusable functions and types that can work with any type, subject to requirements that you define. You can write code that avoids duplication and expresses its intent in a clear, abstracted manner.
Currently it is not supported. But there is a feature request you can support if you would like to see it implemented :) KT-12140
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With