Scala 2.10 带来了不同于 JVM(或者我猜是 CLR)提供的反射。
我们特别需要期待什么?平台将如何改进?
例如,是否会有一个类反射(reflect)语言在字段和访问器方法之间的可转换性,以便我可以迭代对象的属性?
最佳答案
更新2012-07-04:
Daniel SOBRAL (还有on SO)在他的博客文章“JSON serialization with reflection in Scala! Part 1 - So you want to do 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.
更新 2012-06-14。(来自 Eugene Burmako):
在 Scala 2.10.0-M4 ,我们发布了新的反射 API,很可能会在不进行重大更改的情况下进入 2.10.0-final。
有关 API 的更多详细信息,请参阅:
- 所以回答Get companion object instance with new Scala reflection API
- Scala Reflection SIP, June 2012作者:Martin Odersky(SIP,实际上是“Scala Improvement Process”)
- summary and migration route from M3
摘录:
<小时/>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 compilerclassreader
to achieve the same goal).Public reflection API is split into
scala.reflect.base
andscala.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
andscala.reflect.runtime.universe
.
- The former is super lightweight and doesn't involve any classloaders,
- while the latter represents a stripped down compiler.
初步答复,2011 年 9 月:
您可以在Scala GitHub repo中看到reflect包的演变,最近两次提交:
- 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 .
(根据this thread,Liftcode旨在简化“编写代码的代码”)
类(class)scala/reflect/internal/Importers.scala
(昨天创建!)是使用这些最新反射功能的一个很好的例子。
关于scala - Scala 2.10 可以提供哪些反射功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7477248/