31

Scala 2.10 brings reflection other than that provided the JVM (or I guess CLR).

What in particular do we have to look forward to, and how will it improve on the platform?

For example, will there be a class that reflects the language's convertibility between fields and accessor methods, so that I can iterate over the properties of an object?

Duncan McGregor
  • 17,665
  • 12
  • 64
  • 118
  • Hey, I really want examples - there's a bounty now! – Duncan McGregor Sep 21 '11 at 22:59
  • Well, still no real examples, and this question is now Google's top hit for `Scala reflection 2.10` Ho hum, @VonC has it I think, but if anyone can show me user-level code I'd still be grateful. – Duncan McGregor Sep 28 '11 at 20:46
  • Duncan, thank you! See http://stackoverflow.com/questions/7708485/scala-class-getfields/7710418#7710418 Turns out with current 2.10.0-SNAPSHOT you already can iterate over properties of a class. – Nikita Volkov Oct 10 '11 at 08:53

3 Answers3

14

update 2012-07-04:

Daniel SOBRAL (also on SO) details in his blog post "JSON serialization with reflection in Scala! Part 1 - So you want to do reflection?" some of the features coming with reflection:

To recapitulate, Scala 2.10 will come with a Scala reflection library.
That library is used by the compiler itself, but divided into layers through the cake pattern, so different users see different levels of detail, keeping jar sizes adequate to each one's use, and hopefully hiding unwanted detail.

The reflection library also integrates with the upcoming macro facilities, enabling enterprising coders to manipulate code at compile time.


update 2012-06-14. (from Eugene Burmako):
In Scala 2.10.0-M4, we have released the new reflection API that will most likely make it into 2.10.0-final without significant changes.
More details about the API can be found:

Extracts:

Universes and mirrors are now separate entities:

  • universes host reflection artifacts (trees, symbols, types, etc),
  • mirrors abstract loading of those artifacts (e.g. JavaMirror loads stuff using a classloader and annotation unpickler, while GlobalMirror uses internal compiler classreader to achieve the same goal).

Public reflection API is split into scala.reflect.base and scala.reflect.api.

  • The former represents a minimalistic snapshot that is exactly enough to build reified trees and types.
  • To build, but not to analyze - everything smart (for example, getting a type signature) is implemented in scala.reflect.api.

Both reflection domains have their own universe: scala.reflect.basis and scala.reflect.runtime.universe.

  • The former is super lightweight and doesn't involve any classloaders,
  • while the latter represents a stripped down compiler.

Initial answer, Sept. 2011:

You can see evolutions of the reflect package in the Scala GitHub repo, with this two very recent commits:

(Liftcode being, according to this thread, aims at simplifying "writing code that writes code")

The class scala/reflect/internal/Importers.scala (created yesterday!) is a good example of using those latest reflection feature.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
11

Two links which should be of interest:

Personally I am hoping to use this for doing runtime discovery of extensions (i.e. a type that extends a known trait), and generating UI forms and a few other things from those.

Brian Smith
  • 3,383
  • 30
  • 41
5

With current 2.10M4 you already can iterate over members of a class:

reflect.runtime.universe.typeOf[MyClass].members.filter(!_.isMethod)

The above code lists Symbol objects representing members of a class MyClass which are not methods. There are tons of ways you can fine-tune this.

kiritsuku
  • 52,967
  • 18
  • 114
  • 136
Nikita Volkov
  • 42,792
  • 11
  • 94
  • 169