【发布时间】:2021-10-16 13:44:47
【问题描述】:
大家好,我有这样的问题:
implicit class mapValue[T](f: Future[T]){
def mapValue[T]( f: Future[T] )(implicit ec: ExecutionContext): Future[Try[T]] = {
val prom = Promise[Try[T]]()
f onComplete prom.success
prom.future
}
}
implicit class traverseFilteringErrors[A, B](seq: Seq[A])(f: A => Future[B]){ // >*It says implicit class must have a primary constructor with exactly one argument in first parameter list here
def traverseFilteringErrors[A, B](seq: Seq[A])(f: A => Future[B])(implicit ec: ExecutionContext): Future[Seq[B]] = {
Future.traverse( seq )( f andThen mapValue ) map ( _ collect{ case Success( x ) => x } ) // >and Type mismatch. Required: Future[B] => NotInferredA, found: Future[Nothing] => mapValue[Nothing] here.
}
}
上面写着:
隐式类必须有一个主构造函数,在 def traverseFilteringErrors 的第一个参数列表中只有一个参数
和
类型不匹配。必需:Future[B] => NotInferredA,找到:Future[Nothing] => mapValue[Nothing] at f and Then mapValue part
我是 scala 的新手,我应该怎么做才能解决这个问题?
【问题讨论】:
-
什么问题?请解释发生了什么问题。添加相应的错误信息。
-
@talex 已编辑。
-
首先从
def mapValue[T]( f: Future[T] )中移除类型参数——它会覆盖第一类。我也认为这种方法应该是无参数的。在过滤错误方面 - 我想说还有更多 convenient 方式