isn't possible to specify type variable on functio...
# getting-started
e
isn't possible to specify type variable on function references, is it?
y
You can specify the expected type in the form of a val (I think). For instance:
Copy code
fun <T> id(t: T): T = t
val idBool: (Boolean) -> Boolean = ::id
Or even, for the coolness factor:
Copy code
fun main() {
    val foo: () -> MutableMap<String, Int> = ::makeMap
    println(foo().apply {
        this["answer"] = 42
    })
}

fun <K, V> makeMap() = mutableMapOf<K, V>()

fun <T> coerceToType(value: T): T = value
e
this is coming in Kotlin 1.7:
Copy code
fun <T> id(t: T): T = t
fun <R, F> coerceFunction(f: F): F where F : Function<R> = f
val idBool = coerceFunction<Boolean, _>(::id)