This syntax is:
def foo[T: ClassTag] = ...
actually a shorthand for this:
def foo[T](implicit ev: ClassTag[T]) = ...
This means that if a ClassTag[T] was provided for each type parameter of the type, then each function would have an additional hidden parameter or even several.
This, of course, is completely undesirable. One of the most notable drawbacks of this approach is the almost broken Java compatibility when using generics:
SomeClass someClass = new SomeClass(); someClass.foo<Integer>(ClassTag.apply(Integer.class));
Introduce more complex general methods and parameters. And this is only for the possibility of comparing with a common type, which is almost never needed.
Calling standard Java methods will also be mixed. Which method should be called here:
val jc = new JavaClass jc.foo[Int]()
if JavaClass is defined as follows:
public class JavaClass { public <T> void foo() { ... } public <T> void foo(ClassTag<T> ct) { ... } }
?
Of course, if Java supported reified generics, everything would be different ...
source share