为何Kotlin泛型采用类名后置<>、函数名前置<>的语法?
This design choice is intentional, crafted to boost clarity and avoid ambiguity—especially given Kotlin's unique language features:
Alignment with OOP conventions
Most mainstream object-oriented languages (like Java, C#) declare class generic parameters right after the class name. Kotlin follows this pattern to minimize the learning curve for developers switching from those languages. Example:class Foo<T>(val value: T) // Familiar syntax for OOP developersClear separation of function-specific generics
Functions in Kotlin can be top-level (unattached to any class) or nested inside generic classes. Putting generic parameters before the function name makes it instantly obvious these generics belong to the function itself, not the enclosing class. Take this example:class Foo<T>(val value: T) { // <U> is clearly a function-specific generic, not part of Foo's <T> fun <U> combine(other: U): Pair<T, U> = Pair(this.value, other) }If were placed after the function name, distinguishing it from the class's generic parameters would require extra mental effort.
Explicit visual marker for generic functions
For top-level functions, placing generics before the function name acts as a clear signal that this is a generic function—you don't have to scan parameters or return types to figure it out. Example:// Immediately recognizable as a generic function fun <T> logValue(value: T) { println("Value: $value") }Streamlined type inference
Kotlin often infers generic types automatically when calling functions. Placing the generic parameter before the function name lets the compiler quickly link the inferred type to the function, rather than parsing the entire function signature first to identify it as generic.
内容的提问来源于stack exchange,提问作者ntos

