使用数据流时遇到问题。如果我在maven scala插件配置中启用延续,则maven无法编译。
在maven中启用延续后,编译开始失败。错误信息非常混乱。
像这样:
发现:Unit @ scala.util.continuations.cpsSynth @ util.continuations.cps [scala.concurrent.Future [Any]]
必需:单位
或者这个:
发现:((Class [_&lt ;:Indexed [String]],scala.concurrent.Promise [List [_&lt ;:Indexed [String]]]))=> (类[ $ 1(价值$ anonfun)],列表[&lt ;:Indexed [String]])@ scala.util.continuations.cpsSynth @ util.continuations.cps [scala.concurrent.Future [Any]] forSome {type _ $ 1(in value $ anonfun)&lt ;: Indexed [String]}
required:((Class [_&lt ;:Indexed [String]],scala.concurrent.Promise [List [_&lt ;:Indexed [String]]]))=>乙
所有这些错误都发生在关闭处。似乎闭包不能在流程上下文中使用。
任何人都知道为什么?
错误代码:
def toList[T <: Indexed[String]](content: Iterable[String], klass: Class[T]) = {
val ref = new TypeReference[List[T]] {}
content.flatMap(JsonUtils.toObject(_, ref)).toList
}
flow {
val objects = toList(content, klass)
val hasRefs = klass.getFields.filter(it => it.getAnnotation(classOf[Ref]) != null)
hasRefs.foreach {injectRef(_, objects)}
promise << objects
}
def injectRef[T <: Indexed[String]](field: Field, objects: List[T]) {
val anno = field.getAnnotation(classOf[Ref])
val refKlass = anno.klass()
def setField(o: T) {
val original = field.isAccessible
field.setAccessible(true)
val f = field.get(o)
val found = promises.get(refKlass)().find(it => it.index() == f)
field.set(o, found)
field.setAccessible(original)
}
objects.foreach { o =>
setField(o) // here, compile error
}
}