Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What reflection capabilities can we expect from Scala 2.10?

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?

like image 502
Duncan McGregor Avatar asked Sep 19 '11 21:09

Duncan McGregor


People also ask

What is reflection in Scala?

Scala reflection enables a form of metaprogramming which makes it possible for programs to modify themselves at compile time. This compile-time reflection is realized in the form of macros, which provide the ability to execute methods that manipulate abstract syntax trees at compile-time.

What is TypeTag in Scala?

TypeTags#TypeTag . A full type descriptor of a Scala type. For example, a TypeTag[List[String]] contains all type information, in this case, of type scala.

What is Scala manifest?

A Manifest[T] is an opaque descriptor for type T. Its supported use is to give access to the erasure of the type as a Class instance, as is necessary for the creation of native Arrays if the class is not known at compile time.

What is Java reflection?

Reflection is a feature in the Java programming language. It allows an executing Java program to examine or "introspect" upon itself, and manipulate internal properties of the program. For example, it's possible for a Java class to obtain the names of all its members and display them.


2 Answers

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:

  • SO answer Get companion object instance with new Scala reflection API
  • Scala Reflection SIP, June 2012 by Martin Odersky (SIP, actually "Scala Improvement Process")
  • summary and migration route from M3

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:

  • Changes to Liftcode to use new reflection semantics, where a compiler uses type checking.
  • Started work on compiler toolbox that can compile reflect trees at runtime.

(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.

like image 59
VonC Avatar answered Oct 09 '22 11:10

VonC


Two links which should be of interest:

  • The scala-internals mailing list discussion on the reflection api.
  • The nightly build api doc for 2.10-SNAPSHOT.

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.

like image 44
Brian Smith Avatar answered Oct 09 '22 11:10

Brian Smith