두 함수가 있습니다. 하나는 Future[Thing Or Exception]
을 반환하고 다른 하나는 Future[Boolean
을 반환합니다. 모두 호출하고 Future[Thing Or Exception]
을 반환하는 함수가 필요합니다. 부울 함수가 false를 반환하면 예외를 반환하고 그렇지 않으면 다른 함수를 반환합니다.서로 다른 유형의 여러 선물에서 Scala Future를 작성하는 데 문제가 있습니다.
나는 다음과 같이 코드를 가지고있다. 그러나 나는 캐스트와 b)를 싫어한다. 결국 "boolean gets true"경로를 실행할 때 "Promise$DefaultPromise cannot be cast to org.scalatic.Or"
의 결과로 Await.result
이 나오면이 오류가 발생한다.
def thingFuture: Future[Thing Or Exception]
def boolFuture: Future[Boolean]
def combineFutures: Future[Thing Or Exception] = {
val result = boolFuture.map {x =>
x match {
case true => thingFuture
case false => Exception
}
}
// without the cast compiler says result is of type Future[Object]
result.asInstanceOf[Future[Thing Or Exception]]
}
나는이 시도했다 그러나 사람이 어떻게 다른 반환 유형이 선물을 구성하는 방법을 말해 줄래 성공 경로
def combineFutures: Future[Thing Or Exception] = {
val result = boolFuture.map {x =>
x match {
case true => thingFuture.map { y =>
y match {
case Good(thing) => thing
case Bad(exception) => exception
}
case false => Exception
}
}
}
에 같은 약속 오류를 얻는다? 감사!