如何使用 Quasiquotes 声明包
How to declare packages with Quasiquotes
我正在尝试使用准引用来生成包 AST。我有一个字符串变量,它列出了包路径,使得
val pkg = "database.dao"
当我使用准引用 q"package $pkg
时,它告诉我我需要一个 RefTree
。我尝试搜索了一段时间,但仍然没有找到将字符串转换为 RefTree
的答案
我该怎么做?
我在 slideshare
的第 7 页找到了答案
事实证明,如果我想动态注入包变量,我将需要使用多个 Select()
生成我自己的 AST。
def selectIdentity(directory: String): Select = {
val dirs = directory.split('.').reverse
val lastIndex = dirs.length - 1
def apply(i: Int = 0) : Select = if (i < lastIndex - 1) {
Select(apply(i + 1), TermName(dirs(i)))
} else {
Select(Ident(TermName(dirs(lastIndex))), TermName(dirs(lastIndex - 1)))
}
apply()
}
我正在尝试使用准引用来生成包 AST。我有一个字符串变量,它列出了包路径,使得
val pkg = "database.dao"
当我使用准引用 q"package $pkg
时,它告诉我我需要一个 RefTree
。我尝试搜索了一段时间,但仍然没有找到将字符串转换为 RefTree
我该怎么做?
我在 slideshare
的第 7 页找到了答案事实证明,如果我想动态注入包变量,我将需要使用多个 Select()
生成我自己的 AST。
def selectIdentity(directory: String): Select = {
val dirs = directory.split('.').reverse
val lastIndex = dirs.length - 1
def apply(i: Int = 0) : Select = if (i < lastIndex - 1) {
Select(apply(i + 1), TermName(dirs(i)))
} else {
Select(Ident(TermName(dirs(lastIndex))), TermName(dirs(lastIndex - 1)))
}
apply()
}