I believe that in your example there are no advantages. Type parameters are usually used to connect different parts of the code, but information about T
actually lost because it does not correspond to anything. Consider another example:
def f1[T <: Animal](a: T) = (a.sound, a) def f2(a: Animal) = (a.sound, a)
Now everything is different, since T
is passed to the return type:
class Dog extends Animal { def sound = "bow-wow" } f1(new Dog)
In this case, you can think of f1
as a template that is "instantiated" at compile time and effectively generates a specific method based on the types of compilation time parameters. Therefore, if you want to use f1(new Dog)
where it is required (String, Dog)
, it will compile, and f2(new Dog)
will not.
source share