1

Suppose I have a Java class:

public class Foo {
    public static <T> T doSomething(Class<T>) { ... }
    public static <T> T doSomething(TypeToken<T>) { ... }
}

And now I wish to "Scalafy" it. That is, create something like:

object Bar {
    def doSomething[T]: T
}

Apparently, this doesn't compile:

def doSomething[T] = Foo.doSomething(classOf[T])

And doing something like this requires me to limit T to types with no type params:

def doSomething[T: ClassTag] = Foo.doSomething(classTag[T].runtimeClass.asInstanceOf[Class[T]])

Now, of course, you can always funnel everything through the TypeToken method, but suppose that the Class method is cheap and the TypeToken method is expensive. The question I wanted to ask is that is there some kind of user-defined type constraint, akin to =:= or <:<, that I can pass in implicitly to ensure that T has no type parameters (more specifically, classTag[T].runtimeClass is a Class[T] and not some Class[_ >: T])?

Kelvin Chung
  • 1,327
  • 1
  • 11
  • 23

0 Answers0