What does first parameter `<X>` stand for? I...
# announcements
d
What does first parameter
<X>
stand for? It always confused me..
a
d
What I was looking for is generic functions.. but I don't get the point of it.. is this
Copy code
fun <T> singletonList(item: T): List<T> { // ... }
same as this
Copy code
fun singletonList(item: T): List<T> { // ... }
what the removed param stands for? It isn't return type as return type is specified after (args):
a
it specifies that the function call needs to have a T parameter inferred from the context it was called from
If you leave the <T> out in the lines above, then it will simply say it's unknown -- by putting it there you're "declaring" it local to the function declaration
(as opposed to inheriting it from the class the function is a member of or whatever)
specifically in this case, you're specifiying a constraint: the same type you pass as an argument is to be used for the return type
d
I am coming from Java World.. does this stuff exist in Java?
a
yes, Collections.singletonList() has a type parameter there too