https://kotlinlang.org logo
#feed
Title
# feed
a

amanda.hinchman-dominguez

04/18/2019, 3:51 PM
Hello! I had been recommended to post a Kotlin Thursday article here on explaining how reified generics in Kotlin improves upon the generic class-as-a-parameter pattern. I will never be scared at looking at TornadoFX source code again! https://link.medium.com/e7OvFkXnZV
👀 4
🐨 1
👍 10
g

gattacus

04/19/2019, 9:26 AM
using clazz in
Copy code
inline fun <reified T: Mammal> printAnimalResultFiltered(
    list: List<Mammal>,
    factCheck: Mammal.() -> Int
): List<Mammal> {
    if (list.isNotEmpty()) {
        list.forEach {
            if (clazz.isInstance(it)) println("${it.javaClass.name} - ${it.factCheck()}")
        }
    }
    return list
}
doesn't work
a

amanda.hinchman-dominguez

04/22/2019, 10:52 PM
@gattacus - sorry I misread, this is for reified
so
clazz.isInstance
will not apply anymore - I will fix this!
typo I left
ohh this is weird - gist refuses to update
it is saved in my edit, but won't publish. I'll fix this but this is what the code should be
Copy code
inline fun <reified T: Mammal> printAnimalResultFiltered(
    list: List<Mammal>,
    factCheck: Mammal.() -> Int
): List<Mammal> {
    if (list.isNotEmpty()) {
        list.filterIsInstance<T>()
            .forEach {
                println("${it.javaClass.name} - ${it.factCheck()}")
            }
    }
    return list
}

fun main() {
    println("\nSpecies count with list as param:")
    printAnimalResultFiltered<Sloth>(crewCrewCrew, Mammal::knownSpeciesCount)
}
@gattacus it's fixed! Thanks for the typo catch