June 2018
Intermediate to advanced
310 pages
6h 32m
English
Generics are a way to specify the relationships between types. Well, that didn't help explain much, did it? Let's try again. Generics are an abstraction of types. Nope, still awful.
We'll try an example, then:
val listOfStrings = mutableListOf("a", "b", "c")
Ok, that's easy; we've covered it a lot of times. This code simply creates a list of strings. But what does it actually mean?
Let's try the following line of code:
listOfStrings.add(1)
This line doesn't compile. That's because the mutableListOf() function uses generics:
public fun <T> mutableListOf(vararg elements: T): MutableList<T>
Generics create an expectation. No matter which type we use to create our list, from now on we can only put that ...
Read now
Unlock full access