```fun <T> List<Int>.transform( bl...
# getting-started
j
Copy code
fun <T> List<Int>.transform(
    block: (Int) -> T = { it }
): List<T> {
    return map { block(it) }
}

fun main() {
    val items = listOf(1, 2, 3)
    
    println(items.transform { "$it, " })
    println(items.transform())
}
Does anyone know if there is a issue on youtrack somewhere discussing making something like this possible?
a
what’s the expected output of those printlns?
s
So the issue is that the default value for the
block
parameter implies a default value for
T
?
👍 1
j
exactly, but this is not possible with kotlin currently
s
I would just do it with old-fashioned overloads:
Copy code
fun List<Int>.transform(): List<Int> = transform { it }
fun <T> List<Int>.transform(block: (Int) -> T): List<T> = map { block(it) }
❤️ 1
j
Nice and simple solution 🙂 Thanks
but would still be nice to have support for it as a single function