动态混合一个特征 [英] Mixing in a trait dynamically
问题描述
有特点
trait Persisted {
def id: Long
}
如何实现一个方法来接受任何 case 类的实例并返回其混合了 trait 的副本?
how do I implement a method that accepts an instance of any case class and returns its copy with the trait mixed in?
该方法的签名如下:
def toPersisted[T](instance: T, id: Long): T with Persisted
推荐答案
这可以通过宏来完成(从 2.10.0-M3 开始正式成为 Scala 的一部分).这是您要查找的内容的一个要点示例.
This can be done with macros (that are officially a part of Scala since 2.10.0-M3). Here's a gist example of what you are looking for.
1) 我的宏生成一个从提供的案例类和 Persisted 继承的本地类,很像 new T with Persisted
会做的.然后它缓存它的参数(以防止多次评估)并创建所创建类的实例.
1) My macro generates a local class that inherits from the provided case class and Persisted, much like new T with Persisted
would do. Then it caches its argument (to prevent multiple evaluations) and creates an instance of the created class.
2) 我怎么知道要生成什么树?我有一个简单的应用程序 parse.exe,它打印解析输入代码产生的 AST.所以我只是调用 parse class Person$Persisted1(first: String, last: String) extends Person(first, last) with Persisted
,记录输出并在我的宏中复制它.parse.exe 是 scalac -Xprint:parser -Yshow-trees -Ystop-after:parser
的包装器.探索 AST 的方法有多种,请参阅 Scala 2.10 中的元编程".
2) How did I know what trees to generate? I have a simple app, parse.exe that prints the AST that results from parsing input code. So I just invoked parse class Person$Persisted1(first: String, last: String) extends Person(first, last) with Persisted
, noted the output and reproduced it in my macro. parse.exe is a wrapper for scalac -Xprint:parser -Yshow-trees -Ystop-after:parser
. There are different ways to explore ASTs, read more in "Metaprogramming in Scala 2.10".
3) 如果您提供 -Ymacro-debug-lite
作为 scalac 的参数,则可以对宏扩展进行完整性检查.在这种情况下,所有扩展都将被打印出来,您将能够更快地检测代码生成错误.
3) Macro expansions can be sanity-checked if you provide -Ymacro-debug-lite
as an argument to scalac. In that case all expansions will be printed out, and you'll be able to detect codegen errors faster.
编辑.更新了 2.10.0-M7 的例子
edit. Updated the example for 2.10.0-M7
这篇关于动态混合一个特征的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!