【发布时间】:2016-06-12 16:52:08
【问题描述】:
我已经使用 scala 宏功能编写了 JDBC 选择和插入映射。
正如您在要点中看到的那样,有很多重复的代码...... 由于 c.universe._ 导入,我无法在方法范围之外提取案例类 ClassInfo 和 ColumnInfo,因为我使用的是 c.Type。
我曾尝试使用 scala.reflect.runtime.universe._ 但后来我得到了一些镜像异常并且突然类型比较不再起作用 - 我不再获得 ClassSymbol 但 SynchronizedClassSymbol 并且我必须使用 fullName 比较它它突然变得非常混乱......
所以我的问题是:
我想重构:
case class ClassInfo
case class ColumnInfo
mapGet
mapSet
def getBaseClass(tp: c.Type): c.Type
def getClassInfo(tp: c.Type): Option[ClassInfo]
def getConstructorParameters(tp: c.Type): Seq[ColumnInfo]
def selectFullTermName(sym: c.Symbol): c.Tree
在方法 selectImpl 和 insertImpl 之外,但我只是不知道如何:( 例如,我尝试了方法
def getBaseClass(tp: c.Type): c.Type
像这样放在外面:
def getBaseClass(c: whitebox.Context)(tp: c.Type) = {
import c.universe._
if (tp.baseClasses.map(_.fullName).contains(symbolOf[Option[_]].fullName))
tp.typeArgs.head
else
tp
}
但是我称之为
getBaseClass(c)(x.typeSignature)
我收到一个错误:
Type missmatch, expected: c.Type, actual: whitebox.Context#Type
这让我发疯 =)
我是 Scala 的新手,所以如果您有任何关于如何编写宏的其他 cmets - 如果我犯了任何错误 - 请告诉我=)
【问题讨论】: