If you just want the length of any List
, there is no difference for you.
But if you want operatations with the elements of that list, there is.
val l = List(1, 2, 3, 4)
def secondElementAny(l: List[Any]) = l.tail.head
def secondElementGen[A](l : List[A]) = l.tail.head
Giving those two functions and the list, we will expect to get the 2
from the List
.
val secondAny = secondElementAny(l)
val secondGen = secondElementGen(l)
If we would print the values to the console, we could spot no diference, but if we try to cast them to Float
for example, we will get an error. secondAny.toFloat
will tell us, that secondAny is type of Any
and that we cannot use the function toFloat
on Any
.
In contrast secondGen.toFloat
will give us a Float
value.
The reason for that is, that the compiler aproximates the full signatures as follows.
def secondElementAny(l: List[Any]): Any = l.tail.head
def secondElementGen[A](l: List[A]): A = l.tail.head
As you can see the return type of the first function is Any
, so we will always get an Any
whereas the return type of the second function depends on the type of the List
. We will get an element of that type. This is typesafe.