scala - Scala 2.10 可以提供哪些反射功能?

标签 scala reflection

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 的更多详细信息,请参阅:

摘录:

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.
<小时/>

初步答复,2011 年 9 月:

您可以在Scala GitHub repo中看到reflect包的演变,最近两次提交:

(根据this thread,Liftcode旨在简化“编写代码的代码”)

类(class)scala/reflect/internal/Importers.scala (昨天创建!)是使用这些最新反射功能的一个很好的例子。

关于scala - Scala 2.10 可以提供哪些反射功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7477248/

相关文章:

java - 在运行时将包含 java 源的文件夹添加到类路径

scala - Scalatra DSL 使用了哪些 Scala 概念?

scala - 如何让 Intellij 对失败的测试提供文本差异比较

scala - slick 中的编译查询实际上是如何工作的?

c# - C# 中的反射会失败吗?

java - Android源码中setPreferredNetworkType的访问方法

c# - 并非所有程序集都从 bin 文件夹加载到 AppDomain

java - Field#getGenericType() 抛出 java.lang.TypeNotPresentException

ruby - Ruby 中是否有一种优雅的方式在 Scala 中表现得像 `require`?

scala - 提高涉及文件转换的 fs2 流的性能