Hey guys, I would like to share a really trivial thought about the way we declare generic classes and functions in Kotlin.
I am relatively new to the language (having used Kotlin for ~6 months), I have always thought that the way we declare generic classes and generic functions is a bit inconsistent.
// a generic class (type parameter declared after name)
class GenericClass<T> { ... }
// a generic function (type parameter declared before name)
fun <T> genericFunction() { ... }
// a generic extension function (type parameter declared before name)
fun <T> T.genericFunction() { ... }
At first, I thought it looks nicer to declare the type parameters after the function name for consistency, but I realize the possibility of extension functions.
class GenericClass<T> { ... }
// type parameters after the function name, which seems nice...
fun genericFunction<T>() { ... }
// the type parameter is used before it is declared, not good
fun T.genericFunction<T>() { ... }
So, it seems that allowing type parameters declared after function names is not possible. However, I think we could also allow the type parameters to be declared before the class name for consistencies.
// this actually looks nice too
class <T> GenericClass { ... }
// consistent with these
fun <T> genericFunction() { ... }
fun <T> T.genericFunction() { ... }
As a beginner, I have been having a bit of difficulty remembering where is the type parameter list declared when I am writing generic functions and classes. I think this would help haha. What do you guys think?