在SBT中使用宏的问题 [英] Issue with using Macros in SBT
问题描述
假设您有两个SBT项目,一个叫做A,另一个叫做B
Assume you have two SBT projects, one called A and another called B
A有一个称为宏的子项目,该子项目遵循此处显示的完全相同的模式(
A has a subproject called macro, that follows the exact same pattern as showed here (http://www.scala-sbt.org/0.13.0/docs/Detailed-Topics/Macro-Projects.html). In other words, A has a subproject macro with a package that exposes a macro (lets called it macrotools). Now both projects, A and B, use the macrotools package (and A and B are strictly separate projects, B uses A via dependancies in SBT, with A using publish-local)
现在,使用A的macrotools程序包的A很好,一切正常.但是,当B使用A macrotools程序包时,会发生以下错误
Now, A using A's macrotools package is fine, everything works correctly. However when B uses A macrotools package, the following error happens
java.lang.IllegalAccessError: tried to access method com.monetise.waitress.types.Married$.<init>()V from class com.monetise.waitress.types.RelationshipStatus$
对于那些想知道的人,宏就是这个 https://stackoverflow.com/a/13672520/1519631 ,所以换句话说,这个宏就是macrotools软件包中的内容
For those wondering, the macro is this one https://stackoverflow.com/a/13672520/1519631, so in other words, this macro is what is inside the macrotools package
这也与我之前的问题出现在POM/JAR中的宏观依赖性有关,除了我现在使用的是SBT 0.13,而且我遵循的是SBT 0.13的变更指南.
This is also related to my earlier question Macro dependancy appearing in POM/JAR, except that I am now using SBT 0.13, and I am following the altered guide for SBT 0.13
在这种情况下,上面提到的代码就是B中的内容,而A是com.monetise.incredients.macros.tools
(这是build.sbt中指定的依赖项)
The code being referred to above is, in this case, this is what is in B, and A is com.monetise.incredients.macros.tools
(which is a dependency specified in build.sbt)
package com.monetise.waitress.types
import com.monetise.ingredients.macros.tools.SealedContents
sealed abstract class RelationshipStatus(val id:Long, val formattedName:String)
case object Married extends RelationshipStatus(0,"Married")
case object Single extends RelationshipStatus(1,"Single")
object RelationshipStatus {
// val all:Set[RelationshipStatus] = Set(
// Married,Single
// )
val all:Set[RelationshipStatus] = SealedContents.values[RelationshipStatus]
}
如您所见,当我使用注释时,代码可以正常工作(宏的工作是用ADT中的所有case对象填充Set.)当我使用宏版本时,即SealedContents.values[RelationshipStatus]
是当我按下java.lang.IllegalAccessError
As you can see, when I use whats commented, the code works fine (the job of the macro is to fill the Set with all the case objects in an ADT). When I use the macro version, i.e. SealedContents.values[RelationshipStatus]
is when I hit the java.lang.IllegalAccessError
编辑
以下是包含项目的回购 https://github.com/mdedetrich/projecta containsmacro https://github.com/mdedetrich/projectb
Here are the repos containing the projects https://github.com/mdedetrich/projectacontainingmacro https://github.com/mdedetrich/projectb
请注意,我必须做一些更改,这些更改我之前已经忘记了.由于其他项目也需要依赖宏,因此以下两行禁用了宏发布已被注释掉
Note that I had to do some changes, which I forgot about earlier. Because the other project needs to depend on the macro as well, the following 2 lines to disable macro publishing have been commented out
publish := {},
publishLocal := {}
在build.scala中.还要注意,这是一个运行时,而不是编译时错误
In the build.scala. Also note this is a runtime, not a compile time error
编辑2 在 https://github.com/sbt/sbt/issues/874
推荐答案
此问题与SBT无关.看起来像是在Scala中对密封特征进行迭代的宏吗? 您使用的是一个错误.请点击链接以查看修复程序.
This issue is unrelated to SBT. It looks like the macro from Iteration over a sealed trait in Scala? that you're using has a bug. Follow the link to see a fix.
这篇关于在SBT中使用宏的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!