I don't think there's any difference, or reason to prefer one over the other, but wanted to check...
def length(l: List[Any])
def length[T](l: List[T])
I don't think there's any difference, or reason to prefer one over the other, but wanted to check...
def length(l: List[Any])
def length[T](l: List[T])
On
You could also write it like this:
def length(l: List[_])
Now, with regards to your question, List[Any] will, indeed accept any List, but if you had tried Set[Any], it would not work! For instance, try passing a Set[Int] to it:
scala> def length(s: Set[Any]) = s.size
length: (s: Set[Any])Int
scala> val set = Set(1, 2, 3)
set: scala.collection.immutable.Set[Int] = Set(1, 2, 3)
scala> length(set)
<console>:10: error: type mismatch;
found : scala.collection.immutable.Set[Int]
required: Set[Any]
Note: Int <: Any, but trait Set is invariant in type A.
You may wish to investigate a wildcard type such as `_ <: Any`. (SLS 3.2.10)
length(set)
^
List is co-variant, which makes that possible, but not every parameterized class is covariant. Either your parameterized version or the version above will work, though.
I can't think of any reason to prefer one over the other, but I'd rather not parameterize something when it is not needed. If nothing else, it will have a slight positive impact on compilation speed.
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.
Giving those two functions and the list, we will expect to get the
2from theList.If we would print the values to the console, we could spot no diference, but if we try to cast them to
Floatfor example, we will get an error.secondAny.toFloatwill tell us, that secondAny is type ofAnyand that we cannot use the functiontoFloatonAny.In contrast
secondGen.toFloatwill give us aFloatvalue.The reason for that is, that the compiler aproximates the full signatures as follows.
As you can see the return type of the first function is
Any, so we will always get anAnywhereas the return type of the second function depends on the type of theList. We will get an element of that type. This is typesafe.