Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between <T> and <T : Any>

In Kotlin, what is there difference between the following two functions:

fun<T> List<T>.myFunction() { ...
fun<T : Any> List<T>.myOtherFunction() { ...

Presumably this Any constraint is there for a reason, but what is it?

like image 792
AlexC Avatar asked Oct 19 '25 11:10

AlexC


1 Answers

<T>: Without any constraints, it can be any type, including nullable types (e.g., T?), platform types, and non-nullable types

<T : Any>: Must be a non-nullable type. It excludes nullable types and platform types, ensuring that T is a concrete, non-nullable type.

like image 81
H S Progr Avatar answered Oct 21 '25 01:10

H S Progr