I am trying to use the ::: method (is this an operator method?) from List. Okay I know what it is doing now after typing in two lists in the REPL and seeing what was going on. However the API definition of the ::: method was hard to read and understand. I only “got it” by reading what it “returns”.
def :::[B >: A](prefix: List[B]): List[B]
Adds the elements of a given list in front of this list.
prefix The list elements to prepend.
returns list resulting from the concatenation of the given list prefix and this list.
Example: List(1, 2) ::: List(3, 4) = List(3, 4).:::(List(1, 2)) = List(1, 2, 3, 4)
In particular what does this part mean: [B >: A](prefix: List[B]). I mean I was able to understand what the method returns by reading what the method returns and playing with it. For the future, I would like to be able to read the API for a different method and try to understand everything. That is why I am asking this question.
The two answers are both correct. You only have to remember that
Ais the type parameter of your current list, andBis the type parameter of the list you are supplying to:::If you are not yet confident, you can try your own definition of List
And now you can test in the REPL:
So you know should have understood that the ::: concatenates two list and creates a list whose generic type is the first common ancestor. This is because the type list is covariant, so for example you might think that, in case of res3 and res0 the compiler does the following: