1

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?

5
  • Why wouldn't it be? Commented Mar 21, 2020 at 13:03
  • 1
    While the use of Class is 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? Commented Mar 21, 2020 at 14:00
  • @LuisMiguelMejíaSuárez, didn't I explain enough in the question? What else would you like to know? Commented Mar 21, 2020 at 15:18
  • @siledh Why do you want to abstract over functions? What is the end goal? Will the functions will be constructued in runtime or in compiletime? Commented Mar 21, 2020 at 15:27
  • @LuisMiguelMejíaSuárez I'm writing a toy interpreter of a functional language. This abstraction is for a set of built-in functions (that I want to have a flexibility to easily define in my code). Anyway, just after writing the question I realised I won't need this, since the types would be checked before the interpretation so I can just cast the nodes and assume the cast will succeed. But the question is still interesting I think, I would be glad to know how to do this kind of thing in Scala. Commented Mar 21, 2020 at 16:20

1 Answer 1

4

If you don't need Scala-specific details (differentiating between properties and methods, implicits, etc.) using Class is perfectly fine; if it wasn't, it wouldn't be available in scala.Predef as an alias!

If you do, there is Scala reflection but to be honest, in my experience it's harder to use than the Java Class-based reflection. If you can use both I'd prefer Class.

OTOH:

  1. List[Class[Node]] is not correct if you

    expect the arguments to be different subtypes of Node

    It should be List[Class[_ <: Node]] instead.

  2. Class only reflects classes after erasure, e.g. classOf[List[String]] and classOf[List[Int]] are the same. If you need to differentiate them Class won't be enough.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.