androidkotlingeneric-type-parameters

Kotlin Function with Genetic T Type


During a project development, I have encountered this weird function syntax

fun <T: Any> boo() { do something}

I am aware of T indicates a generic type and Any is a root class of all class in Kotlin and : implies class inheritance or interface implementation but what is T: Any?


Solution

  • T : SomeType sets the upper bound of the generic type. T would have to either be SomeType or be some sub-type of SomeType.

    In this case, T : Any sets the upper bound to be Any. While that may seem pointless, it does mean that T cannot be nullable.