在 Scala 编译器插件中创建新的*包*
为了在 Scala 编译器插件中生成新代码,我现在创建了工作类。下一个逻辑步骤是将这些类放入一个新的、不存在的包中。在 Java 中,包基本上是一个目录名称,但在 Scala 中,包看起来要复杂得多。到目前为止,我还没有找到/识别编译器插件创建新包的示例。
以我目前的理解水平,我认为我需要首先使用以下命令创建一个包符号:
parentPackage.newPackage(...)
// ...
然后使用 PackageDef 为该包创建一个树。但是 PackageDef 并不像人们所期望的那样将符号作为参数,并且搜索:
Scala newPackage PackageDef
没有返回任何有用的内容。所以看来我不需要同时执行这两个步骤。编译器可能为我完成了其中一项,但我不知道是哪一项。到目前为止,我所拥有的看起来像这样:
val newPkg = parentPackage.newPackage(NoPosition, newTermName(name))
newPkg.moduleClass.setInfo(new PackageClassInfoType(new Scope,
newPkg.moduleClass))
newPkg.setInfo(newPkg.moduleClass.tpe)
parentPackage.info.decls.enter(newPkg)
// ...
val newPkgTree = PackageDef(Ident(newPkg.name), List(ClassDef(...)))
In my quest to generate new code in a Scala compiler plugin, I have now created working classes. The next logical step is to put those classes in a new, non-existing package. In Java, a package is basically a directory name, but in Scala a package seems much more complicated. So far I haven't found/recognized an example where a compiler plugin creates a new package.
At my current level of understanding, I would think that I would need to create first a package symbol with:
parentPackage.newPackage(...)
// ...
and than later create a Tree for the package with PackageDef. But PackageDef doesn't take the symbol as parameter, as one would expect, and searching for:
Scala newPackage PackageDef
returned nothing useful. So it seems that I don't need to do those two steps together. Possibly one is done for my by the compiler, but I don't know which one. So far, what I have looks like this:
val newPkg = parentPackage.newPackage(NoPosition, newTermName(name))
newPkg.moduleClass.setInfo(new PackageClassInfoType(new Scope,
newPkg.moduleClass))
newPkg.setInfo(newPkg.moduleClass.tpe)
parentPackage.info.decls.enter(newPkg)
// ...
val newPkgTree = PackageDef(Ident(newPkg.name), List(ClassDef(...)))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为我对你的另一个问题的回答也应该回答这个问题:
如何在 Scala 编译器插件中添加新类?
I think my answer to your other question should answer this one as well:
How to add a new Class in a Scala Compiler Plugin?