gpt4 book ai didi

scala - "implementation restriction: nested class is not allowed in value class This restriction is planned to be removed in subsequent releases."

转载 作者:行者123 更新时间:2023-12-04 11:07:25 26 4
gpt4 key购买 nike

方法说明:

Given a list of futures fs, returns the future holding the list of values of all the futures from fs. The returned future is completed only once all of the futures in fs have been completed. The values in the list are in the same order as corresponding futures fs. If any of the futures fs fails, the resulting future also fails.



这就是我所做的:
implicit class FutureCompanionOps[T](val f: Future.type) extends AnyVal {
/***

//Some others method

***/
def all[T](fs: List[Future[T]]): Future[List[T]] = async {
var fsVar = fs;
val l = List()
while(fsVar.nonEmpty){
await{fsVar.head} :: l
fsVar = fsVar.tail
}
l
}
}

但是我在这一行收到以下编译错误 def all[T](fs: List[Future[T]]): Future[List[T]] = async { :

implementation restriction: nested class is not allowed in value class This restriction is planned to be removed in subsequent releases.



有人可以向我解释这个错误并向我展示一个解决方法吗?

最佳答案

从常见问题解答:

Scala 异步功能即将发布第一个版本,部分处于实验阶段。

作为在值类中使用 Scala Async 的解决方法,请考虑使用以下技巧将异步调用移到值类之外:

class FutureOps[T](f: Future[T]) extends AnyVal {
def foo: Future[T] = fooImpl(f)
}

def fooImpl(f: Future[T]) = async {
val x = await { f }
throw new Exception
}

如果那个或其他代码杂耍和重构不能解决问题,请考虑使用常规功能组合器,如 map , flatMap , continueWith和他们的其他 friend 。

关于scala - "implementation restriction: nested class is not allowed in value class This restriction is planned to be removed in subsequent releases.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20252687/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com