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.

5
  • 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). Commented Feb 4, 2010 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.. Commented Feb 5, 2010 at 2: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. Commented Feb 5, 2010 at 2:51
  • Related stackoverflow.com/q/7477248/97777 Commented Oct 10, 2011 at 6:52
  • i just posted about the new reflection api here => 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. Commented Mar 7, 2012 at 13:47

1 Answer 1

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.

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

1 Comment

This fails at runtime because of casting, not because of structural types.

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.