In Java, there is a Class<T> class that represents, well, classes. It's useful when you need to do some class checks dynamically at runtime, for example, if some interface can accept varied number of arguments and it expects the arguments to have specific types: then the interface can return a list of Class objects to specify its requirements.
Apparently, it's available in Scala as well, but just as an alias to the Java class). Is it OK to use this in Scala (or in general, the Class class, in pure Scala code), or would there be a more idiomatic way to achieve the same thing there?
For more context, this is what I'm thinking of:
trait BuiltInFunction[Node] {
def symbol: String
def arity: Int
def argumentTypes: List[Class[Node]]
def apply(args: List[Node]): Node
}
BuiltInFunction represents functions that each can have different number of arguments and expect the arguments to be different subtypes of Node. If the system encounters a function, it will make sure that it's applied to arguments of proper types (as defined by argumentTypes) by calling the apply method with those arguments.
Is using Class the right way to do it in Scala?
Classis valid in Scala, relfection is not that common and usually there are other alternatives which are faster & safer. Would you mind explaining what do you want to do?