12

Does anyone know the status of a fully-featured reflection API for Scala?

I know that you can use Java's reflection API to do simple things but this does not work well with Scala's language features. I found an interesting article describing an experimental Scala Mirroring API but as far as I know this is still experimental. I've also found mention of a ScalaSigParser but this seems to be pretty low level.

This is more of a curiosity than anything else as I am currently just playing around with Scala. I thought that the answer to this question might also be useful to others interested in Scala.

Travis Schneeberger
  • 2,010
  • 20
  • 23
  • Can you be more specific about what you want reflection to do? You can use routines in scala.tools.nsc to help with the name-mangling, and some things simply don't reflect well--you really need compilation to help you out because they're not just a matter of calling existing methods (e.g. if you need a new closure). – Rex Kerr Feb 04 '10 at 23:45
  • First, I was thinking it would be nice to create instances of classes with named parameters such that you do not have to worry about the order of the parameters - this could be an immutable replacement for the JavaBean style pattern. I was also thinking it would be nice to be able to look at the structure of a class as it pertains to Scala features. ie. get me all the ctors - find the primary one - call it. Or answer questions like is this field a val or var. Plus it it would be nice to use a reflection API done in idiomatic Scala style rather than falling back to Java's API. Just ideas.. – Travis Schneeberger Feb 05 '10 at 02:39
  • I was not thinking of some of the more dynamic metaprogramming features like adding methods at runtime or eval style statements - just more along the lines of what the Java reflection API allows you to do but adapted for the Scala language. – Travis Schneeberger Feb 05 '10 at 02:51
  • Related http://stackoverflow.com/q/7477248/97777 – Duncan McGregor Oct 10 '11 at 06:52
  • i just posted about the new reflection api here => http://alots.wordpress.com/ I did a demonstration about how to use the new reflection api to discover if a method is implicit or not. There are some links pointing directly for the scala github repo. – Alberto Souza Mar 07 '12 at 13:47

1 Answers1

6

The "immutable replacement for the JavaBean style pattern" can be expressed named parameters and optionally the @BeanProperty annotation:

import reflect._
case class A(@BeanProperty val x: String, @BeanProperty val y : Int)

A(x = "s", y = 3)
A(y = 3, x = "s")

Adding methods (more precise: defining a new interface) makes only sense in a statically typed language if the client knowns about the new methods and can compile against the interface. With structural typing clients can define methods they expect to be present in an object. The Scala compiler will transform the structural type into reflection code which may fail at runtime.

type T = {def go(x : Int): Int }
def y(any : Any) = any.asInstanceOf[T].go(2)

class A{
  def go(x : Int) = x + 1
}
y(new A())
y(new {}) //this will fail

You can define new classes or traits with the interpreter on the fly. The Interpret method transforms Scala code to byte code.

You've already mentioned the ScalaSigParser which is not exactly easy to work with.

I think the rest of features you like are not there yet.

Community
  • 1
  • 1
Thomas Jung
  • 32,428
  • 9
  • 84
  • 114