Signature of a generic function in Kotlin

Asked

Viewed 83 times

1

I was watching the part about Generics and I’d like to know why the

fun <T> addToList(vararg  arg : T): ArrayList<T> {

and not the

fun addToList(vararg  arg : T): ArrayList<T> {

1 answer

1


The exact reason would have to ask who created this syntax. My "polite kick" is that it would be more complicated to do the parser otherwise. He might consider that the T that appears in the argument is even a guy called T and not a generic signature variable and this would be at least ambiguous. With the initial indication, before any use in the signature, we T is a generic type, its later use makes it clear that it is the slot generic type and not a concrete type called T.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.